vector_scal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. unsigned i;
  27. float *factor = _args;
  28. starpu_vector_interface_t *vector = buffers[0];
  29. unsigned n = STARPU_VECTOR_GET_NX(vector);
  30. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  31. FPRINTF(stderr, "running task with %d CPUs.\n", starpu_combined_worker_get_size());
  32. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  33. for (i = 0; i < n; i++)
  34. val[i] *= *factor;
  35. }
  36. static struct starpu_perfmodel_t vector_scal_model = {
  37. .type = STARPU_HISTORY_BASED,
  38. .symbol = "vector_scale_parallel"
  39. };
  40. static starpu_codelet cl = {
  41. .where = STARPU_CPU,
  42. .type = STARPU_FORKJOIN,
  43. .max_parallelism = INT_MAX,
  44. .cpu_func = scal_cpu_func,
  45. .nbuffers = 1,
  46. .model = &vector_scal_model,
  47. };
  48. int main(int argc, char **argv)
  49. {
  50. struct starpu_conf conf;
  51. float vector[NX];
  52. unsigned i;
  53. for (i = 0; i < NX; i++)
  54. vector[i] = (i+1.0f);
  55. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  56. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  57. starpu_conf_init(&conf);
  58. /* Most OpenMP implementations do not support concurrent parallel
  59. * sections, so only create one big worker */
  60. conf.single_combined_worker = 1;
  61. starpu_init(&conf);
  62. starpu_data_handle vector_handle;
  63. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  64. float factor = 3.14;
  65. struct starpu_task *task = starpu_task_create();
  66. task->synchronous = 1;
  67. task->cl = &cl;
  68. task->buffers[0].handle = vector_handle;
  69. task->buffers[0].mode = STARPU_RW;
  70. task->cl_arg = &factor;
  71. task->cl_arg_size = sizeof(factor);
  72. starpu_task_submit(task);
  73. starpu_data_unregister(vector_handle);
  74. starpu_task_destroy(task);
  75. /* terminate StarPU, no task can be submitted after */
  76. starpu_shutdown();
  77. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  78. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  79. return 0;
  80. }