|
| 1 | +/* |
| 2 | + * Basic DGEMM test |
| 3 | + * |
| 4 | + * Multiply two matrices of dimensions SIZE*SIZE filled with ones. Therefore, |
| 5 | + * all the elements of the resulting matrix will be just SIZE. |
| 6 | + */ |
| 7 | + |
| 8 | +#define SIZE 1024 |
| 9 | +#define REPEAT 30 |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <unistd.h> |
| 13 | +#include <thread> |
| 14 | +#include <mutex> |
| 15 | +#include <vector> |
| 16 | +#include <algorithm> |
| 17 | +#include <functional> |
| 18 | + |
| 19 | +#include "Xdevice/runtime.hpp" |
| 20 | +#include "Xdevice/blas.hpp" |
| 21 | + |
| 22 | + |
| 23 | +namespace kernels |
| 24 | +{ |
| 25 | + template<class T> |
| 26 | + __global__ void init_as_ones(T * arr, size_t size) |
| 27 | + { |
| 28 | + unsigned int tid = threadIdx.x + blockIdx.x*blockDim.x; |
| 29 | + if (tid < size) |
| 30 | + { |
| 31 | + arr[tid] = (T)1.0; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + template<class T> |
| 36 | + __global__ void verify(T * arr, size_t size, int * err) |
| 37 | + { |
| 38 | + unsigned int tid = threadIdx.x + blockIdx.x*blockDim.x; |
| 39 | + if (tid < size) |
| 40 | + { |
| 41 | + if (int(arr[tid]) != SIZE) |
| 42 | + atomicAdd(err, 1); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | + * This code uses a thread per device in the node. |
| 49 | + * For simplicity, we define the variables below as global. |
| 50 | + */ |
| 51 | + |
| 52 | +#define HOST_NAME_SIZE 128 |
| 53 | +char hostname[HOST_NAME_SIZE]; |
| 54 | +double tflops = SIZE*SIZE*SIZE*2.0 * 1E-12; |
| 55 | +int totalErrors = 0; |
| 56 | +std::mutex mtx; |
| 57 | + |
| 58 | +#define BLOCK_SIZE 128 |
| 59 | +void dgemm(int device) |
| 60 | +{ |
| 61 | + XSetDevice(device); |
| 62 | + |
| 63 | + double * A; |
| 64 | + double * B; |
| 65 | + double * C; |
| 66 | + const double alpha = 1.0; |
| 67 | + const double beta = 0.0; |
| 68 | + |
| 69 | + XMalloc((void**)&A, sizeof(double)*SIZE*SIZE); |
| 70 | + XMalloc((void**)&B, sizeof(double)*SIZE*SIZE); |
| 71 | + XMalloc((void**)&C, sizeof(double)*SIZE*SIZE); |
| 72 | + |
| 73 | + kernels::init_as_ones<double><<<(SIZE*SIZE+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(A, SIZE*SIZE); |
| 74 | + kernels::init_as_ones<double><<<(SIZE*SIZE+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(B, SIZE*SIZE); |
| 75 | + XDeviceSynchronize(); |
| 76 | + |
| 77 | + XStream_t stream; |
| 78 | + XStreamCreate(&stream); |
| 79 | + XblasHandle_t blas_handle; |
| 80 | + XblasCreate(&blas_handle); |
| 81 | + XblasSetStream(blas_handle, stream); |
| 82 | + |
| 83 | + // Warmup call |
| 84 | + XblasDgemm(blas_handle, |
| 85 | + XBLAS_OP_N, XBLAS_OP_N, |
| 86 | + SIZE, SIZE, SIZE, |
| 87 | + &alpha, |
| 88 | + (const double*)A, SIZE, |
| 89 | + (const double*)B, SIZE, |
| 90 | + &beta, |
| 91 | + C, SIZE); |
| 92 | + XDeviceSynchronize(); |
| 93 | + |
| 94 | + // Time the execution |
| 95 | + XTimer t(stream); |
| 96 | + t.start(); |
| 97 | + for (int i = 0; i < REPEAT; i++) |
| 98 | + { |
| 99 | + XblasDgemm(blas_handle, |
| 100 | + XBLAS_OP_N, XBLAS_OP_N, |
| 101 | + SIZE, SIZE, SIZE, |
| 102 | + &alpha, |
| 103 | + (const double*)A, SIZE, |
| 104 | + (const double*)B, SIZE, |
| 105 | + &beta, |
| 106 | + C, SIZE); |
| 107 | + } |
| 108 | + |
| 109 | + // Calc the performance data in TFlops/sec |
| 110 | + double perf = tflops/(t.stop()/REPEAT/1000.0); |
| 111 | + |
| 112 | + XblasDestroy(blas_handle); |
| 113 | + XStreamDestroy(stream); |
| 114 | + |
| 115 | + // Verify that the final values of C are correct. |
| 116 | + int * err, h_err = 0; |
| 117 | + XMalloc((void**)&err, sizeof(int)); |
| 118 | + XMemcpy( err, &h_err, sizeof(int), XMemcpyHostToDevice); |
| 119 | + kernels::verify<double><<<(SIZE+BLOCK_SIZE-1)/BLOCK_SIZE, BLOCK_SIZE>>>(C, SIZE*SIZE, err); |
| 120 | + XMemcpy(&h_err, err, sizeof(int), XMemcpyDeviceToHost); |
| 121 | + { |
| 122 | + std::lock_guard<std::mutex> lg(mtx); |
| 123 | + totalErrors += h_err; |
| 124 | + |
| 125 | + // Print the performance results |
| 126 | + printf("[%s] GPU %d: %4.2f TF/s\n", hostname, device, (float)perf); |
| 127 | + } |
| 128 | + XFree(A); |
| 129 | + XFree(B); |
| 130 | + XFree(C); |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +int main(int argc, char **argv) |
| 135 | +{ |
| 136 | + |
| 137 | + gethostname(hostname, sizeof(hostname)); |
| 138 | + |
| 139 | + int num_devices; |
| 140 | + XGetDeviceCount(&num_devices); |
| 141 | + |
| 142 | + // Print device count |
| 143 | + printf("[%s] Found %d device(s).\n", hostname, num_devices); |
| 144 | + |
| 145 | + // Create vector of threads. |
| 146 | + std::vector<std::thread> threads; |
| 147 | + |
| 148 | + // Do the dgemm for all devices in the node. |
| 149 | + for (int device = 0; device < num_devices; device++) |
| 150 | + { |
| 151 | + threads.push_back(std::thread(dgemm,device)); |
| 152 | + } |
| 153 | + |
| 154 | + // Join all threads |
| 155 | + std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join)); |
| 156 | + |
| 157 | + // Test if there were any errors and print the test result. |
| 158 | + printf("[%s] Test %s\n", hostname, totalErrors == 0 ? "passed" : "failed"); |
| 159 | + |
| 160 | + return 0; |
| 161 | +} |
0 commit comments