vector_scal_omp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010-2013 Université de Bordeaux 1
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /* gcc build:
  18. gcc -fopenmp -O2 -g vector_scal.c -o vector_scal $(pkg-config --cflags starpu-1.0) $(pkg-config --libs starpu-1.0)
  19. */
  20. #include <starpu.h>
  21. #include <stdio.h>
  22. #include <limits.h>
  23. #ifdef STARPU_QUICK_CHECK
  24. #define NX 2048
  25. #else
  26. #define NX 2048000
  27. #endif
  28. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  29. void scal_cpu_func(void *buffers[], void *_args)
  30. {
  31. unsigned i;
  32. float *factor = _args, f = *factor;
  33. struct starpu_vector_interface *vector = buffers[0];
  34. unsigned n = STARPU_VECTOR_GET_NX(vector);
  35. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  36. FPRINTF(stderr, "running task with %d CPUs.\n", starpu_combined_worker_get_size());
  37. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  38. for (i = 0; i < n; i++) {
  39. float v = val[i];
  40. int j;
  41. for (j = 0; j < 100; j++)
  42. v = v * f;
  43. val[i] = v;
  44. }
  45. }
  46. static struct starpu_perfmodel vector_scal_model =
  47. {
  48. .type = STARPU_HISTORY_BASED,
  49. .symbol = "vector_scal_parallel"
  50. };
  51. static struct starpu_codelet cl =
  52. {
  53. .modes = { STARPU_RW },
  54. .type = STARPU_FORKJOIN,
  55. .max_parallelism = INT_MAX,
  56. .cpu_funcs = {scal_cpu_func, NULL},
  57. .cpu_funcs_name = {"scal_cpu_func", NULL},
  58. .nbuffers = 1,
  59. .model = &vector_scal_model,
  60. };
  61. int main(int argc, char **argv)
  62. {
  63. struct starpu_conf conf;
  64. float *vector;
  65. unsigned i;
  66. int ret;
  67. vector = malloc(NX*sizeof(*vector));
  68. for (i = 0; i < NX; i++)
  69. vector[i] = (i+1.0f);
  70. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  71. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  72. starpu_conf_init(&conf);
  73. /* Most OpenMP implementations do not support concurrent parallel
  74. * sections, so only enable one combined worker at a time. */
  75. conf.single_combined_worker = 1;
  76. conf.sched_policy_name = "pheft";
  77. ret = starpu_init(&conf);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  79. starpu_data_handle_t vector_handle;
  80. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  81. float factor = 1.001;
  82. for (i = 0; i < 100; i++)
  83. {
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &cl;
  86. task->handles[0] = vector_handle;
  87. task->cl_arg = &factor;
  88. task->cl_arg_size = sizeof(factor);
  89. ret = starpu_task_submit(task);
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  91. }
  92. starpu_data_unregister(vector_handle);
  93. /* terminate StarPU, no task can be submitted after */
  94. starpu_shutdown();
  95. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  96. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  97. return 0;
  98. }