parallel_kernels_spmd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #define N 1000
  18. #define VECTORSIZE 1024
  19. static pthread_mutex_t mutex;
  20. static pthread_cond_t cond;
  21. static unsigned finished = 0;
  22. static unsigned cnt;
  23. starpu_data_handle v_handle;
  24. static unsigned *v;
  25. static void codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  26. {
  27. int worker_size = starpu_combined_worker_get_size();
  28. assert(worker_size > 0);
  29. usleep(1000/worker_size);
  30. #if 0
  31. int id = starpu_worker_get_id();
  32. int combined_id = starpu_combined_worker_get_id();
  33. int rank = starpu_combined_worker_get_rank();
  34. fprintf(stderr, "worker id %d - combined id %d - worker size %d - SPMD rank %d\n", id, combined_id, worker_size, rank);
  35. #endif
  36. }
  37. struct starpu_perfmodel_t model = {
  38. .type = STARPU_HISTORY_BASED,
  39. .symbol = "parallel_kernel_test_spmd"
  40. };
  41. static starpu_codelet cl = {
  42. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  43. .type = STARPU_SPMD,
  44. .max_parallelism = INT_MAX,
  45. .cpu_func = codelet_null,
  46. .cuda_func = codelet_null,
  47. .opencl_func = codelet_null,
  48. .model = &model,
  49. .nbuffers = 1
  50. };
  51. int main(int argc, char **argv)
  52. {
  53. struct starpu_conf conf = {
  54. .sched_policy_name = "pheft",
  55. .ncpus = -1,
  56. .ncuda = -1,
  57. .nopencl = -1,
  58. .nspus = -1,
  59. .use_explicit_workers_bindid = 0,
  60. .use_explicit_workers_cuda_gpuid = 0,
  61. .use_explicit_workers_opencl_gpuid = 0,
  62. .calibrate = 1
  63. };
  64. starpu_init(&conf);
  65. starpu_data_malloc_pinned_if_possible((void **)&v, VECTORSIZE*sizeof(unsigned));
  66. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  67. unsigned nworker = starpu_worker_get_count() + starpu_combined_worker_get_count();
  68. cnt = nworker*N;
  69. unsigned iter, worker;
  70. for (iter = 0; iter < N; iter++)
  71. {
  72. for (worker = 0; worker < nworker; worker++)
  73. {
  74. /* execute a task on that worker */
  75. struct starpu_task *task = starpu_task_create();
  76. task->cl = &cl;
  77. task->buffers[0].handle = v_handle;
  78. task->buffers[0].mode = STARPU_R;
  79. int ret = starpu_task_submit(task);
  80. if (ret == -ENODEV)
  81. goto enodev;
  82. }
  83. }
  84. starpu_task_wait_for_all();
  85. starpu_shutdown();
  86. return 0;
  87. enodev:
  88. fprintf(stderr, "WARNING: No one can execute this task\n");
  89. /* yes, we do not perform the computation but we did detect that no one
  90. * could perform the kernel, so this is not an error from StarPU */
  91. return 0;
  92. }