policy_selection2.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015, 2016, 2017 CNRS
  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_mpi.h>
  17. #include "helper.h"
  18. void func_cpu(void *descr[], void *_args)
  19. {
  20. (void)_args;
  21. int *data0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  22. int *data1 = (int *)STARPU_VARIABLE_GET_PTR(descr[1]);
  23. int *data2 = (int *)STARPU_VARIABLE_GET_PTR(descr[2]);
  24. *data1 += *data0;
  25. *data2 += *data0;
  26. }
  27. /* Dummy cost function for simgrid */
  28. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  29. {
  30. return 0.000001;
  31. }
  32. static struct starpu_perfmodel dumb_model =
  33. {
  34. .type = STARPU_COMMON,
  35. .cost_function = cost_function
  36. };
  37. struct starpu_codelet mycodelet =
  38. {
  39. .cpu_funcs = {func_cpu},
  40. .nbuffers = 3,
  41. .modes = {STARPU_R, STARPU_W, STARPU_W},
  42. .model = &dumb_model
  43. };
  44. int main(int argc, char **argv)
  45. {
  46. int ret;
  47. int i;
  48. int rank, size;
  49. int data[3];
  50. starpu_data_handle_t handles[3];
  51. int mpi_init;
  52. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  53. (void)mpi_init;
  54. ret = starpu_init(NULL);
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  56. ret = starpu_mpi_init(NULL, NULL, 0);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  58. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  59. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  60. if ((size < 3) || (starpu_cpu_worker_get_count() == 0))
  61. {
  62. if (rank == 0)
  63. {
  64. if (size < 3)
  65. FPRINTF(stderr, "We need at least 3 processes.\n");
  66. else
  67. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  68. }
  69. starpu_mpi_shutdown();
  70. starpu_shutdown();
  71. if (!mpi_init)
  72. MPI_Finalize();
  73. return STARPU_TEST_SKIPPED;
  74. }
  75. data[0] = 12;
  76. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&data[0], sizeof(int));
  77. starpu_mpi_data_register(handles[0], 10, 0);
  78. data[1] = 12;
  79. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)&data[1], sizeof(int));
  80. starpu_mpi_data_register(handles[1], 20, 1);
  81. data[2] = 12;
  82. starpu_variable_data_register(&handles[2], STARPU_MAIN_RAM, (uintptr_t)&data[2], sizeof(int));
  83. starpu_mpi_data_register(handles[2], 30, 2);
  84. starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  85. STARPU_R, handles[2], STARPU_W, handles[0], STARPU_W, handles[1],
  86. 0);
  87. for(i=0 ; i<2 ; i++) starpu_data_acquire(handles[i], STARPU_R);
  88. FPRINTF_MPI(stderr, "data[%d,%d,%d] = %d,%d,%d\n", 0, 1, 2, data[0], data[1], data[2]);
  89. for(i=0 ; i<2 ; i++) starpu_data_release(handles[i]);
  90. #ifndef STARPU_SIMGRID
  91. if (rank == 2)
  92. {
  93. STARPU_ASSERT_MSG(data[0] == 2*data[2] && data[1] == 2*data[2], "Computation incorrect. data[%d] (%d) != 2*data[%d] (%d) && data[%d] (%d) != 2*data[%d] (%d)\n",
  94. 0, data[0], 2, data[2], 1, data[1], 2, data[2]);
  95. }
  96. #endif
  97. for(i=0 ; i<2 ; i++) starpu_data_acquire(handles[i], STARPU_W);
  98. for(i=0 ; i<2 ; i++) data[i] = 12;
  99. for(i=0 ; i<2 ; i++) starpu_data_release(handles[i]);
  100. // Let StarPU choose the node
  101. starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  102. STARPU_R, handles[2], STARPU_W, handles[0], STARPU_W, handles[1],
  103. STARPU_EXECUTE_ON_NODE, 1,
  104. 0);
  105. for(i=0 ; i<2 ; i++) starpu_data_acquire(handles[i], STARPU_R);
  106. FPRINTF_MPI(stderr, "data[%d,%d,%d] = %d,%d,%d\n", 0, 1, 2, data[0], data[1], data[2]);
  107. for(i=0 ; i<2 ; i++) starpu_data_release(handles[i]);
  108. #ifndef STARPU_SIMGRID
  109. if (rank == 1)
  110. {
  111. STARPU_ASSERT_MSG(data[0] == 2*data[2] && data[1] == 2*data[2], "Computation incorrect. data[%d] (%d) != 2*data[%d] (%d) && data[%d] (%d) != 2*data[%d] (%d)\n",
  112. 0, data[0], 2, data[2], 1, data[1], 2, data[2]);
  113. }
  114. #endif
  115. for(i=0 ; i<3 ; i++) starpu_data_unregister(handles[i]);
  116. starpu_mpi_shutdown();
  117. starpu_shutdown();
  118. if (!mpi_init)
  119. MPI_Finalize();
  120. return 0;
  121. }