vector_scal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 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. for (i = 0; i < NX; i++)
  58. vector[i] = (i+1.0f);
  59. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  60. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  61. starpu_conf_init(&conf);
  62. /* Most OpenMP implementations do not support concurrent parallel
  63. * sections, so only create one big worker */
  64. conf.single_combined_worker = 1;
  65. starpu_init(&conf);
  66. starpu_data_handle_t vector_handle;
  67. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  68. float factor = 3.14;
  69. struct starpu_task *task = starpu_task_create();
  70. task->synchronous = 1;
  71. task->cl = &cl;
  72. task->handles[0] = vector_handle;
  73. task->cl_arg = &factor;
  74. task->cl_arg_size = sizeof(factor);
  75. starpu_task_submit(task);
  76. starpu_data_unregister(vector_handle);
  77. starpu_task_destroy(task);
  78. /* terminate StarPU, no task can be submitted after */
  79. starpu_shutdown();
  80. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  81. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  82. return 0;
  83. }