Host code No CUDA here, only MPI
| 59 | // Host code |
| 60 | // No CUDA here, only MPI |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | // Dimensions of the dataset |
| 64 | int blockSize = 256; |
| 65 | int gridSize = 10000; |
| 66 | int dataSizePerNode = gridSize * blockSize; |
| 67 | |
| 68 | // Initialize MPI state |
| 69 | MPI_CHECK(MPI_Init(&argc, &argv)); |
| 70 | |
| 71 | // Get our MPI node number and node count |
| 72 | int commSize, commRank; |
| 73 | MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &commSize)); |
| 74 | MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &commRank)); |
| 75 | |
| 76 | // Generate some random numbers on the root node (node 0) |
| 77 | int dataSizeTotal = dataSizePerNode * commSize; |
| 78 | float *dataRoot = NULL; |
| 79 | |
| 80 | // Are we the root node? |
| 81 | if (commRank == 0) { |
| 82 | cout << "Running on " << commSize << " nodes" << endl; |
| 83 | dataRoot = new float[dataSizeTotal]; |
| 84 | initData(dataRoot, dataSizeTotal); |
| 85 | } |
| 86 | |
| 87 | // Allocate a buffer on each node |
| 88 | float *dataNode = new float[dataSizePerNode]; |
| 89 | |
| 90 | // Dispatch a portion of the input data to each node |
| 91 | MPI_CHECK( |
| 92 | MPI_Scatter(dataRoot, dataSizePerNode, MPI_FLOAT, dataNode, dataSizePerNode, MPI_FLOAT, 0, MPI_COMM_WORLD)); |
| 93 | |
| 94 | if (commRank == 0) { |
| 95 | // No need for root data any more |
| 96 | delete[] dataRoot; |
| 97 | } |
| 98 | |
| 99 | // On each node, run computation on GPU |
| 100 | computeGPU(dataNode, blockSize, gridSize); |
| 101 | |
| 102 | // Reduction to the root node, computing the sum of output elements |
| 103 | float sumNode = sum(dataNode, dataSizePerNode); |
| 104 | float sumRoot; |
| 105 | |
| 106 | MPI_CHECK(MPI_Reduce(&sumNode, &sumRoot, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD)); |
| 107 | |
| 108 | if (commRank == 0) { |
| 109 | float average = sumRoot / dataSizeTotal; |
| 110 | cout << "Average of square roots is: " << average << endl; |
| 111 | } |
| 112 | |
| 113 | // Cleanup |
| 114 | delete[] dataNode; |
| 115 | MPI_CHECK(MPI_Finalize()); |
| 116 | |
| 117 | if (commRank == 0) { |
| 118 | cout << "PASSED\n"; |
nothing calls this directly
no test coverage detected