vector_scal.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010, 2011 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 vector_scal.c -o vector_scal $(pkg-config --cflags libstarpu) $(pkg-config --libs libstarpu)
  19. */
  20. #include <starpu.h>
  21. #include <stdio.h>
  22. #include <limits.h>
  23. #define NX 2048
  24. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  25. void scal_cpu_func(void *buffers[], void *_args)
  26. {
  27. unsigned i;
  28. float *factor = _args;
  29. struct starpu_vector_interface *vector = buffers[0];
  30. unsigned n = STARPU_VECTOR_GET_NX(vector);
  31. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  32. FPRINTF(stderr, "running task with %d CPUs.\n", starpu_combined_worker_get_size());
  33. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  34. for (i = 0; i < n; i++)
  35. val[i] *= *factor;
  36. }
  37. static struct starpu_perfmodel vector_scal_model =
  38. {
  39. .type = STARPU_HISTORY_BASED,
  40. .symbol = "vector_scale_parallel"
  41. };
  42. static struct starpu_codelet cl =
  43. {
  44. .modes = { STARPU_RW },
  45. .where = STARPU_CPU,
  46. .type = STARPU_FORKJOIN,
  47. .max_parallelism = INT_MAX,
  48. .cpu_funcs = {scal_cpu_func, NULL},
  49. .nbuffers = 1,
  50. .model = &vector_scal_model,
  51. };
  52. int main(int argc, char **argv)
  53. {
  54. struct starpu_conf conf;
  55. float vector[NX];
  56. unsigned i;
  57. int ret;
  58. for (i = 0; i < NX; i++)
  59. vector[i] = (i+1.0f);
  60. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  61. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  62. starpu_conf_init(&conf);
  63. /* Most OpenMP implementations do not support concurrent parallel
  64. * sections, so only create one big worker */
  65. conf.single_combined_worker = 1;
  66. ret = starpu_init(&conf);
  67. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  68. starpu_data_handle_t vector_handle;
  69. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  70. float factor = 3.14;
  71. struct starpu_task *task = starpu_task_create();
  72. task->synchronous = 1;
  73. task->cl = &cl;
  74. task->handles[0] = vector_handle;
  75. task->cl_arg = &factor;
  76. task->cl_arg_size = sizeof(factor);
  77. ret = starpu_task_submit(task);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  79. starpu_data_unregister(vector_handle);
  80. starpu_task_destroy(task);
  81. /* terminate StarPU, no task can be submitted after */
  82. starpu_shutdown();
  83. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  84. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  85. return 0;
  86. }