#include "convolution.h" #include #include #include "util.h" #include #define CUDA_CALL(f) \ { \ cudaError_t err = (f); \ if (err != cudaSuccess) { \ fprintf(stderr, "CUDA error at [%s:%d] %d %s\n", __FILE__, __LINE__, \ err, cudaGetErrorString(err)); \ exit(1); \ } \ } #define TS_X 4 #define TS_Y 4 #define TS_Z 64 #define MAX_NUM_GPU 4 int num_devices = 0; static float *input_d[MAX_NUM_GPU]; static float *filter_d[MAX_NUM_GPU]; static float *output_d[MAX_NUM_GPU]; static int Mbegin[MAX_NUM_GPU], Mend[MAX_NUM_GPU]; void convolution_cuda(); void convolution_cuda_init(int,int); void convolution_cuda_final(int); static float *input, *output, *filter; static int N, C, H, W; static int K, R, S; static int OH, OW; static int pad; static int dilation; static int stride; static int mpi_rank, mpi_world_size; int p_n, remain, offset; MPI_Status status; MPI_Request request; __global__ void conv_kernel(float *input, float *filter, float *output, int N, int C, int H, int W, int K, int R, int S, int OH, int OW, int pad, int dilation, int stride) { int k = blockDim.x * blockIdx.x + threadIdx.x; int oh = blockDim.y * blockIdx.y + threadIdx.y; int ow = blockDim.z * blockIdx.z + threadIdx.z; if (k >= K || oh>= OH || ow >= OW) return; for(int n = 0; n < N; n++){ float o = 0.f; for (int c = 0; c < C; ++c) { for (int r = 0; r < R; ++r) { for (int s = 0; s < S; ++s) { int h = oh * stride - pad + r * dilation; int w = ow * stride - pad + s * dilation; if (h < 0 || h >= H || w < 0 || w >= W) continue; float i = input[n * C * H * W + c * H * W + h * W + w]; float f = filter[k * C * R * S + c * R * S + r * S + s]; o += i * f; } } } output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o; } } void convolution_cuda() { // Launch kernel on every GPU for (int i = 0; i < num_devices; i++) { dim3 blockDim(TS_X, TS_Y, TS_Z); dim3 gridDim((K+TS_X-1)/TS_X, (OH+TS_Y-1)/TS_Y, (OW+TS_Z-1)/TS_Z); CUDA_CALL( cudaSetDevice(i) ); conv_kernel<<>>(input_d[i], filter_d[i], output_d[i], Mend[i] - Mbegin[i], C, H ,W , K, R, S, OH, OW, pad, dilation, stride ); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaDeviceSynchronize() ); } } void convolution_cuda_init() { CUDA_CALL( cudaGetDeviceCount(&num_devices) ); printf("Using %d devices\n", num_devices); for (int i = 0; i < num_devices; i++) { cudaDeviceProp prop; CUDA_CALL( cudaGetDeviceProperties(&prop, i) ); // Try printing more detailed information here printf("[GPU %d] %s\n", i, prop.name); } if (num_devices <= 0) { printf("No CUDA device found. Aborting\n"); exit(1); } // Setup problem size for each GPU for (int i = 0; i < num_devices; i++) { Mbegin[i] = (p_n / num_devices) * i; Mend[i] = (p_n / num_devices) * (i + 1); } Mend[num_devices - 1] = p_n; // Allocate device memory for each GPU for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaMalloc(&input_d[i], (Mend[i] - Mbegin[i]) * C * H * W * sizeof(float)) ); CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) ); CUDA_CALL( cudaMalloc(&output_d[i], (Mend[i] - Mbegin[i]) * K* OH * OW * sizeof(float)) ); } // Upload A and B matrix to every GPU for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaMemcpy(input_d[i], input + Mbegin[i] * C * H * W, (Mend[i] - Mbegin[i]) * C * H * W * sizeof(float), cudaMemcpyHostToDevice) ); CUDA_CALL( cudaMemcpy(filter_d[i], filter, K * C * R * S * sizeof(float), cudaMemcpyHostToDevice) ); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaDeviceSynchronize() ); } } void convolution_cuda_final() { // Do any post-matmul cleanup work here. // Download C matrix from GPUs for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaMemcpy(output + Mbegin[i] * K * OH * OW, output_d[i], (Mend[i] - Mbegin[i]) * K * OH * OW * sizeof(float), cudaMemcpyDeviceToHost) ); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaDeviceSynchronize() ); } } void convolution( float *_input, float *_output, float *_filter, int _N, int _C, int _H, int _W, int _K, int _R, int _S, int _pad, int _dilation, int _stride) { input = _input; output = _output; filter = _filter; p_n = N/mpi_world_size; remain= N%mpi_world_size; offset = remain; OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1; OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1; if (mpi_rank == 0) { for (int dest=1; dest < mpi_world_size; dest++){ offset += p_n; MPI_Isend(&offset, 1, MPI_INT, dest , 1, MPI_COMM_WORLD,&request); MPI_Isend(&p_n, 1, MPI_INT, dest , 1, MPI_COMM_WORLD,&request); MPI_Isend(&input[offset*C*H*W], p_n*C*H*W, MPI_FLOAT, dest , 1, MPI_COMM_WORLD,&request); MPI_Isend(filter, K*C*R*S, MPI_FLOAT, dest, 1, MPI_COMM_WORLD,&request); } p_n += remain; convolution_cuda_init(); convolution_cuda(); convolution_cuda_final(); for (int source=1; source < mpi_world_size; source++){ MPI_Recv(&offset, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status); MPI_Recv(&p_n, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status); MPI_Recv(&output[offset*K*OH*OW], p_n*K*OH*OW, MPI_FLOAT, source, 2, MPI_COMM_WORLD, &status); } } if(mpi_rank > 0){ alloc_tensor(&input, p_n, C,H,W); alloc_tensor(&filter, K,C,R,S); alloc_tensor(&output, p_n, K,OH,OW); MPI_Recv(&offset, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); MPI_Recv(&p_n, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); MPI_Recv(input, p_n*C*H*W, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status); MPI_Recv(filter, K*C*R*S, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status); convolution_cuda_init(); convolution_cuda(); convolution_cuda_final(); MPI_Send(&offset, 1, MPI_INT, 0, 2, MPI_COMM_WORLD); MPI_Send(&p_n, 1, MPI_INT, 0, 2, MPI_COMM_WORLD); MPI_Send(output, p_n*K*OH*OW, MPI_FLOAT, 0, 2, MPI_COMM_WORLD); } } void convolution_init( int _N, int _C, int _H, int _W, int _K, int _R, int _S, int _pad, int _dilation, int _stride) { N = _N; C = _C; H = _H; W = _W; K = _K; R = _R; S = _S; pad = _pad; dilation = _dilation; stride = _stride; MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size); } void convolution_final( int _N, int _C, int _H, int _W, int _K, int _R, int _S, int _pad, int _dilation, int _stride) { }