|
| 1 | +#ifdef __MPI |
| 2 | +#include "source_base/parallel_device.h" |
| 3 | + |
| 4 | +#include "source_base/communication_domain.h" |
| 5 | + |
| 6 | +#include "gtest/gtest.h" |
| 7 | +#include <complex> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace |
| 11 | +{ |
| 12 | + |
| 13 | +template <typename T> |
| 14 | +void exercise_cpu_point() |
| 15 | +{ |
| 16 | + const int count = 2; |
| 17 | + T values[count] = {static_cast<T>(1), static_cast<T>(2)}; |
| 18 | + T temporary[count] = {}; |
| 19 | + Parallel_Common::object_cpu_point<T, base_device::DEVICE_CPU> point; |
| 20 | + |
| 21 | + EXPECT_EQ(point.get_buffer(values, count), values); |
| 22 | + EXPECT_EQ(point.get(values, count), values); |
| 23 | + EXPECT_EQ(point.get_buffer(values, count, temporary), values); |
| 24 | + EXPECT_EQ(point.get(values, count, temporary), values); |
| 25 | + point.sync_h2d(values, temporary, count); |
| 26 | + point.sync_d2h(temporary, values, count); |
| 27 | + point.del(values); |
| 28 | +} |
| 29 | + |
| 30 | +template <typename T> |
| 31 | +void exercise_gpu_staging_stubs() |
| 32 | +{ |
| 33 | + const int count = 2; |
| 34 | + T values[count] = {static_cast<T>(1), static_cast<T>(2)}; |
| 35 | + T temporary[count] = {}; |
| 36 | + Parallel_Common::object_cpu_point<T, base_device::DEVICE_GPU> point; |
| 37 | + |
| 38 | + EXPECT_EQ(point.get_buffer(values, count, temporary), temporary); |
| 39 | + EXPECT_EQ(point.get(values, count, temporary), temporary); |
| 40 | + point.sync_h2d(values, temporary, count); |
| 41 | + point.sync_d2h(temporary, values, count); |
| 42 | + point.del(temporary); |
| 43 | + |
| 44 | + T* allocated = point.get_buffer(values, count); |
| 45 | + EXPECT_NE(allocated, nullptr); |
| 46 | + point.del(allocated); |
| 47 | +} |
| 48 | + |
| 49 | +template <typename T> |
| 50 | +void exercise_mpi_wrappers(const ModuleBase::CommunicationDomain& domain) |
| 51 | +{ |
| 52 | + MPI_Comm communicator = domain.communicator(); |
| 53 | + const int rank = domain.rank(); |
| 54 | + const int size = domain.size(); |
| 55 | + const int count = 2; |
| 56 | + |
| 57 | + T sent[count] = {static_cast<T>(rank + 1), static_cast<T>(rank + 2)}; |
| 58 | + T received[count] = {}; |
| 59 | + MPI_Status status; |
| 60 | + MPI_Request request; |
| 61 | + Parallel_Common::send_dev<T, base_device::DEVICE_CPU>(sent, count, MPI_PROC_NULL, 0, communicator); |
| 62 | + Parallel_Common::isend_dev<T, base_device::DEVICE_CPU>(sent, |
| 63 | + count, |
| 64 | + MPI_PROC_NULL, |
| 65 | + 0, |
| 66 | + communicator, |
| 67 | + &request, |
| 68 | + nullptr); |
| 69 | + Parallel_Common::recv_dev<T, base_device::DEVICE_CPU>(received, count, MPI_PROC_NULL, 0, communicator, &status); |
| 70 | + |
| 71 | + T broadcast[count] = {}; |
| 72 | + if (rank == 0) |
| 73 | + { |
| 74 | + broadcast[0] = static_cast<T>(3); |
| 75 | + broadcast[1] = static_cast<T>(5); |
| 76 | + } |
| 77 | + Parallel_Common::bcast_dev<T, base_device::DEVICE_CPU>(broadcast, count, communicator, 0); |
| 78 | + EXPECT_EQ(broadcast[0], static_cast<T>(3)); |
| 79 | + EXPECT_EQ(broadcast[1], static_cast<T>(5)); |
| 80 | + |
| 81 | + T reduced[count] = {static_cast<T>(rank + 1), static_cast<T>(2 * (rank + 1))}; |
| 82 | + Parallel_Common::reduce_dev<T, base_device::DEVICE_CPU>(reduced, count, communicator); |
| 83 | + const T sum = static_cast<T>(size * (size + 1) / 2); |
| 84 | + EXPECT_EQ(reduced[0], sum); |
| 85 | + EXPECT_EQ(reduced[1], static_cast<T>(2) * sum); |
| 86 | + |
| 87 | + const T gathered_value = static_cast<T>(rank + 1); |
| 88 | + std::vector<T> gathered(size); |
| 89 | + std::vector<int> receive_counts(size, 1); |
| 90 | + std::vector<int> displacements(size); |
| 91 | + for (int index = 0; index < size; ++index) |
| 92 | + { |
| 93 | + displacements[index] = index; |
| 94 | + } |
| 95 | + Parallel_Common::gatherv_dev<T, base_device::DEVICE_CPU>(&gathered_value, |
| 96 | + 1, |
| 97 | + gathered.data(), |
| 98 | + receive_counts.data(), |
| 99 | + displacements.data(), |
| 100 | + communicator); |
| 101 | + for (int index = 0; index < size; ++index) |
| 102 | + { |
| 103 | + EXPECT_EQ(gathered[index], static_cast<T>(index + 1)); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +TEST(ParallelDevice, CoversCpuPointSpecializations) |
| 108 | +{ |
| 109 | + exercise_cpu_point<float>(); |
| 110 | + exercise_cpu_point<double>(); |
| 111 | + exercise_cpu_point<std::complex<float>>(); |
| 112 | + exercise_cpu_point<std::complex<double>>(); |
| 113 | +} |
| 114 | + |
| 115 | +TEST(ParallelDevice, CoversGpuStagingWithoutAccelerator) |
| 116 | +{ |
| 117 | + exercise_gpu_staging_stubs<float>(); |
| 118 | + exercise_gpu_staging_stubs<double>(); |
| 119 | + exercise_gpu_staging_stubs<std::complex<float>>(); |
| 120 | + exercise_gpu_staging_stubs<std::complex<double>>(); |
| 121 | +} |
| 122 | + |
| 123 | +TEST(ParallelDevice, CoversMpiTypeOverloads) |
| 124 | +{ |
| 125 | + const ModuleBase::CommunicationDomain domain = ModuleBase::world_communication_domain(); |
| 126 | + exercise_mpi_wrappers<float>(domain); |
| 127 | + exercise_mpi_wrappers<double>(domain); |
| 128 | + exercise_mpi_wrappers<std::complex<float>>(domain); |
| 129 | + exercise_mpi_wrappers<std::complex<double>>(domain); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace |
| 133 | +#endif |
0 commit comments