vector_scal_omp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  4. * Copyright (C) 2010-2013 Université de Bordeaux
  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. {
  40. float v = val[i];
  41. int j;
  42. for (j = 0; j < 100; j++)
  43. v = v * f;
  44. val[i] = v;
  45. }
  46. }
  47. static struct starpu_perfmodel vector_scal_model =
  48. {
  49. .type = STARPU_HISTORY_BASED,
  50. .symbol = "vector_scal_parallel"
  51. };
  52. static struct starpu_codelet cl =
  53. {
  54. .modes = { STARPU_RW },
  55. .type = STARPU_FORKJOIN,
  56. .max_parallelism = INT_MAX,
  57. .cpu_funcs = {scal_cpu_func},
  58. .cpu_funcs_name = {"scal_cpu_func"},
  59. .nbuffers = 1,
  60. .model = &vector_scal_model,
  61. };
  62. int main(int argc, char **argv)
  63. {
  64. struct starpu_conf conf;
  65. float *vector;
  66. unsigned i;
  67. int ret;
  68. vector = malloc(NX*sizeof(*vector));
  69. for (i = 0; i < NX; i++)
  70. vector[i] = (i+1.0f);
  71. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  72. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  73. starpu_conf_init(&conf);
  74. /* Most OpenMP implementations do not support concurrent parallel
  75. * sections, so only enable one combined worker at a time. */
  76. conf.single_combined_worker = 1;
  77. conf.sched_policy_name = "pheft";
  78. ret = starpu_init(&conf);
  79. if (ret == -ENODEV) return 77;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. starpu_data_handle_t vector_handle;
  82. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  83. float factor = 1.001;
  84. for (i = 0; i < 100; i++)
  85. {
  86. struct starpu_task *task = starpu_task_create();
  87. task->cl = &cl;
  88. task->handles[0] = vector_handle;
  89. task->cl_arg = &factor;
  90. task->cl_arg_size = sizeof(factor);
  91. ret = starpu_task_submit(task);
  92. if (ret == -ENODEV) goto enodev;
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  94. }
  95. starpu_data_unregister(vector_handle);
  96. /* terminate StarPU, no task can be submitted after */
  97. starpu_shutdown();
  98. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  99. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  100. free(vector);
  101. return 0;
  102. enodev:
  103. starpu_data_unregister(vector_handle);
  104. free(vector);
  105. starpu_shutdown();
  106. return 77;
  107. }