gpu_register.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <starpu_opencl.h>
  18. #include <starpu_cuda.h>
  19. #include "../helper.h"
  20. #include "scal.h"
  21. int main(int argc, char **argv)
  22. {
  23. int ret;
  24. #ifdef STARPU_USE_CUDA
  25. #if CUDART_VERSION >= 4000
  26. unsigned *foo_gpu;
  27. unsigned *foo;
  28. starpu_data_handle_t handle;
  29. int n, i, size, pieces;
  30. int devid;
  31. unsigned workerid;
  32. int chosen = -1;
  33. cudaError_t cures;
  34. #endif
  35. #endif
  36. ret = starpu_init(NULL);
  37. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  38. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  39. #ifdef STARPU_USE_CUDA
  40. #if CUDART_VERSION >= 4000 /* We need thread-safety of CUDA */
  41. /* TODO OpenCL, too */
  42. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  43. if (starpu_worker_get_type(workerid) == STARPU_CUDA_WORKER) {
  44. chosen = workerid;
  45. break;
  46. }
  47. }
  48. if (chosen == -1)
  49. return STARPU_TEST_SKIPPED;
  50. #ifdef STARPU_USE_OPENCL
  51. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/scal_opencl.cl", &opencl_program, NULL);
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  53. #endif
  54. n = starpu_worker_get_count();
  55. size = 10 * n;
  56. devid = starpu_worker_get_devid(chosen);
  57. cudaSetDevice(devid);
  58. cudaMalloc((void**)&foo_gpu, size * sizeof(*foo_gpu));
  59. foo = calloc(size, sizeof(*foo));
  60. for (i = 0; i < size; i++)
  61. foo[i] = i;
  62. cures = cudaMemcpy(foo_gpu, foo, size * sizeof(*foo_gpu), cudaMemcpyHostToDevice);
  63. if (STARPU_UNLIKELY(cures))
  64. STARPU_CUDA_REPORT_ERROR(cures);
  65. starpu_vector_data_register(&handle, starpu_worker_get_memory_node(chosen), (uintptr_t)foo_gpu, size, sizeof(*foo_gpu));
  66. /* Broadcast the data to force in-place partitioning */
  67. for (i = 0; i < n; i++)
  68. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(i), 0);
  69. /* Even with just one worker, split in at least two */
  70. if (n == 1)
  71. pieces = 2;
  72. else
  73. pieces = n;
  74. struct starpu_data_filter f =
  75. {
  76. .filter_func = starpu_block_filter_func_vector,
  77. .nchildren = pieces,
  78. };
  79. starpu_data_partition(handle, &f);
  80. for (i = 0; i < pieces; i++) {
  81. struct starpu_task *task = starpu_task_create();
  82. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  83. task->cl = &scal_codelet;
  84. task->execute_on_a_specific_worker = 1;
  85. task->workerid = i%n;
  86. ret = starpu_task_submit(task);
  87. if (ret == -ENODEV) goto enodev;
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  89. }
  90. ret = starpu_task_wait_for_all();
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  92. starpu_data_unpartition(handle, starpu_worker_get_memory_node(chosen));
  93. starpu_data_unregister(handle);
  94. cudaSetDevice(devid);
  95. cures = cudaMemcpy(foo, foo_gpu, size * sizeof(*foo_gpu), cudaMemcpyDeviceToHost);
  96. if (STARPU_UNLIKELY(cures))
  97. STARPU_CUDA_REPORT_ERROR(cures);
  98. starpu_shutdown();
  99. for (i = 0; i < size; i++) {
  100. if (foo[i] != i*2) {
  101. fprintf(stderr,"value %d is %d instead of %d\n", i, foo[i], 2*i);
  102. return EXIT_FAILURE;
  103. }
  104. }
  105. return EXIT_SUCCESS;
  106. enodev:
  107. starpu_data_unregister(handle);
  108. #endif
  109. #endif
  110. fprintf(stderr, "WARNING: No one can execute this task\n");
  111. /* yes, we do not perform the computation but we did detect that no one
  112. * could perform the kernel, so this is not an error from StarPU */
  113. starpu_shutdown();
  114. return STARPU_TEST_SKIPPED;
  115. }