#include "convolution.h" #include #include #include "util.h" #include #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 MAX_NUM_GPU 4 #define TS 8 __global__ void conv( 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, int OH, int OW) { const int globalRow = blockDim.x * blockIdx.x + threadIdx.x; const int globalCol = blockDim.y * blockIdx.y + threadIdx.y; int n, k; n = globalCol/(_K*OW); k = (globalCol-n*(_K*OW))/OW; int col = (globalCol-n*(_K*OW))-k*OW; int row = globalRow; if (globalRow >= OH || globalCol >= _N*_K*OW) return; int start_row = row *_stride - _pad; int start_col = col *_stride - _pad; float o = 0.0f; for(int c = 0; c<_C; c++) { for (int r = 0; r<_R; r++) { for (int s = 0; s < _S; ++s) { int h = start_row + r * _dilation; int w = start_col + 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 + row * OW + col] = o; } static float *input, *output, *filter; static float *input_d[MAX_NUM_GPU]; static float *filter_d[MAX_NUM_GPU]; static float *output_d[MAX_NUM_GPU]; static int N, C, H, W; static int K, R, S; static int pad; static int dilation; static int stride; static int mpi_rank, mpi_world_size; int num_devices = 0; static int size[2]; static int Mbegin[MAX_NUM_GPU], NN[MAX_NUM_GPU]; static int OH, OW; 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) { MPI_Request request; MPI_Status status; input = _input; output = _output; filter = _filter; if (size[1] != 0) { if (mpi_rank == 0) { MPI_Isend(&input[size[0]*C*H*W], size[1]*C*H*W, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request); MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request); } else { alloc_tensor(&input, size[1], C, H, W); alloc_tensor(&output, size[1], K, OH, OW); alloc_tensor(&filter, K, C, R, S); MPI_Recv(input, size[1]*C*H*W, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status); MPI_Recv(filter, K*C*R*S, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status); } } if (size[mpi_rank] != 0) { for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaMemcpy(input_d[i], input + Mbegin[i]*C*H*W, NN[i]*C*H*W * sizeof(float), cudaMemcpyHostToDevice) ); CUDA_CALL( cudaMemcpy(filter_d[i], filter, K*C*R*S * sizeof(float), cudaMemcpyHostToDevice) ); } // Launch kernel on every GPU for (int i = 0; i < num_devices; i++) { dim3 gridDim((OH+TS-1)/TS, (NN[i]*K*OW + TS - 1)/TS, 1); dim3 blockDim(TS, TS, 1); CUDA_CALL( cudaSetDevice(i) ); conv<<>>(input_d[i], output_d[i], filter_d[i], NN[i], _C, _H, _W, _K, _R, _S, _pad, _dilation, _stride, OH, OW); } // 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], NN[i] * K*OH*OW * sizeof(float), cudaMemcpyDeviceToHost) ); } } if(size[1] != 0) { if (mpi_rank == 0) { MPI_Irecv(&output[size[0]*K*OH*OW], size[1]*K*OH*OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request); } else { MPI_Isend(output, size[1]*K*OH*OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request); } MPI_Wait(&request, &status); } } 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); if (mpi_world_size == 2 && _N > 4) size[1] = _N / 2; else size[1] = 0; size[0] = N - size[1]; if (size[mpi_rank] < MAX_NUM_GPU) { num_devices = size[mpi_rank]; for (int i = 0 ; i < size[mpi_rank] ; i++) { NN[i] = 1; Mbegin[i] = i; } } else { num_devices = MAX_NUM_GPU; int q = size[mpi_rank] / MAX_NUM_GPU; int r = size[mpi_rank] % MAX_NUM_GPU; int sum = 0; for (int i = 0 ; i < MAX_NUM_GPU ; i++) { NN[i] = q; Mbegin[i] = sum; if (i == MAX_NUM_GPU - 1) { NN[i] += r; } sum += NN[i]; } } OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1; OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1; // possigle to MPI CUDA_CALL( cudaGetDeviceCount(&num_devices) ); for (int i = 0; i < num_devices; i++) { cudaDeviceProp prop; CUDA_CALL( cudaGetDeviceProperties(&prop, i) ); } if (num_devices <= 0) { exit(1); } // Allocate device memory for each GPU for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaMalloc(&input_d[i], NN[i]*C*H*W * sizeof(float)) ); CUDA_CALL( cudaMalloc(&filter_d[i], K*C*R*S * sizeof(float)) ); CUDA_CALL( cudaMalloc(&output_d[i], NN[i]*K*OH*OW * sizeof(float)) ); } } void convolution_final( int _N, int _C, int _H, int _W, int _K, int _R, int _S, int _pad, int _dilation, int _stride) { }