vector_scal_omp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2017 CNRS
  4. * Copyright (C) 2010-2013, 2015 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. /*
  18. * This shows how to use an OpenMP parallel implementation for a StarPU
  19. * forkjoin parallel task.
  20. * This is just a vector scaling example.
  21. */
  22. /* gcc build:
  23. gcc -fopenmp -O2 -g vector_scal.c -o vector_scal $(pkg-config --cflags starpu-1.0) $(pkg-config --libs starpu-1.0)
  24. */
  25. #include <starpu.h>
  26. #include <stdio.h>
  27. #include <limits.h>
  28. #ifdef STARPU_QUICK_CHECK
  29. #define NX 2048
  30. #else
  31. #define NX 2048000
  32. #endif
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. void scal_cpu_func(void *buffers[], void *_args)
  35. {
  36. unsigned i;
  37. float *factor = _args, f = *factor;
  38. struct starpu_vector_interface *vector = buffers[0];
  39. unsigned n = STARPU_VECTOR_GET_NX(vector);
  40. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  41. FPRINTF(stderr, "running task with %d CPUs.\n", starpu_combined_worker_get_size());
  42. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  43. for (i = 0; i < n; i++)
  44. {
  45. float v = val[i];
  46. int j;
  47. for (j = 0; j < 100; j++)
  48. v = v * f;
  49. val[i] = v;
  50. }
  51. }
  52. static struct starpu_perfmodel vector_scal_model =
  53. {
  54. .type = STARPU_HISTORY_BASED,
  55. .symbol = "vector_scal_parallel"
  56. };
  57. static struct starpu_codelet cl =
  58. {
  59. .modes = { STARPU_RW },
  60. .type = STARPU_FORKJOIN,
  61. .max_parallelism = INT_MAX,
  62. .cpu_funcs = {scal_cpu_func},
  63. .cpu_funcs_name = {"scal_cpu_func"},
  64. .nbuffers = 1,
  65. .model = &vector_scal_model,
  66. };
  67. int main(void)
  68. {
  69. struct starpu_conf conf;
  70. float *vector;
  71. unsigned i;
  72. int ret;
  73. vector = malloc(NX*sizeof(*vector));
  74. for (i = 0; i < NX; i++)
  75. vector[i] = (i+1.0f);
  76. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  77. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  78. starpu_conf_init(&conf);
  79. /* Most OpenMP implementations do not support concurrent parallel
  80. * sections, so only enable one combined worker at a time. */
  81. conf.single_combined_worker = 1;
  82. conf.sched_policy_name = "pheft";
  83. ret = starpu_init(&conf);
  84. if (ret == -ENODEV) return 77;
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  86. starpu_data_handle_t vector_handle;
  87. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  88. float factor = 1.001;
  89. for (i = 0; i < 100; i++)
  90. {
  91. struct starpu_task *task = starpu_task_create();
  92. task->cl = &cl;
  93. task->handles[0] = vector_handle;
  94. task->cl_arg = &factor;
  95. task->cl_arg_size = sizeof(factor);
  96. ret = starpu_task_submit(task);
  97. if (ret == -ENODEV) goto enodev;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  99. }
  100. starpu_data_unregister(vector_handle);
  101. /* terminate StarPU, no task can be submitted after */
  102. starpu_shutdown();
  103. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  104. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  105. free(vector);
  106. return 0;
  107. enodev:
  108. starpu_data_unregister(vector_handle);
  109. free(vector);
  110. starpu_shutdown();
  111. return 77;
  112. }