vector_scal_omp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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. /*
  17. * This shows how to use an OpenMP parallel implementation for a StarPU
  18. * forkjoin parallel task.
  19. * This is just a vector scaling example.
  20. */
  21. /* gcc build:
  22. gcc -fopenmp -O2 -g vector_scal.c -o vector_scal $(pkg-config --cflags starpu-1.0) $(pkg-config --libs starpu-1.0)
  23. */
  24. #include <starpu.h>
  25. #include <stdio.h>
  26. #include <limits.h>
  27. #ifdef STARPU_QUICK_CHECK
  28. #define NX 2048
  29. #else
  30. #define NX 2048000
  31. #endif
  32. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  33. void scal_cpu_func(void *buffers[], void *_args)
  34. {
  35. unsigned i;
  36. float *factor = _args, f = *factor;
  37. struct starpu_vector_interface *vector = buffers[0];
  38. unsigned n = STARPU_VECTOR_GET_NX(vector);
  39. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  40. FPRINTF(stderr, "running task with %d CPUs.\n", starpu_combined_worker_get_size());
  41. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  42. for (i = 0; i < n; i++)
  43. {
  44. float v = val[i];
  45. int j;
  46. for (j = 0; j < 100; j++)
  47. v = v * f;
  48. val[i] = v;
  49. }
  50. }
  51. static struct starpu_perfmodel vector_scal_model =
  52. {
  53. .type = STARPU_HISTORY_BASED,
  54. .symbol = "vector_scal_parallel"
  55. };
  56. static struct starpu_codelet cl =
  57. {
  58. .modes = { STARPU_RW },
  59. .type = STARPU_FORKJOIN,
  60. .max_parallelism = INT_MAX,
  61. .cpu_funcs = {scal_cpu_func},
  62. .cpu_funcs_name = {"scal_cpu_func"},
  63. .nbuffers = 1,
  64. .model = &vector_scal_model,
  65. };
  66. int main(void)
  67. {
  68. struct starpu_conf conf;
  69. float *vector;
  70. unsigned i;
  71. int ret;
  72. vector = malloc(NX*sizeof(*vector));
  73. for (i = 0; i < NX; i++)
  74. vector[i] = (i+1.0f);
  75. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  76. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  77. starpu_conf_init(&conf);
  78. /* Most OpenMP implementations do not support concurrent parallel
  79. * sections, so only enable one combined worker at a time. */
  80. conf.single_combined_worker = 1;
  81. conf.sched_policy_name = "pheft";
  82. ret = starpu_init(&conf);
  83. if (ret == -ENODEV) return 77;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  85. starpu_data_handle_t vector_handle;
  86. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  87. float factor = 1.001;
  88. for (i = 0; i < 100; i++)
  89. {
  90. struct starpu_task *task = starpu_task_create();
  91. task->cl = &cl;
  92. task->handles[0] = vector_handle;
  93. task->cl_arg = &factor;
  94. task->cl_arg_size = sizeof(factor);
  95. ret = starpu_task_submit(task);
  96. if (ret == -ENODEV) goto enodev;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  98. }
  99. starpu_data_unregister(vector_handle);
  100. /* terminate StarPU, no task can be submitted after */
  101. starpu_shutdown();
  102. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  103. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  104. free(vector);
  105. return 0;
  106. enodev:
  107. starpu_data_unregister(vector_handle);
  108. free(vector);
  109. starpu_shutdown();
  110. return 77;
  111. }