vector_scal_omp.c 3.7 KB

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