#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 2 //#define TS_Y 2 //#define TS_Z 32 #define TS_X 2 #define TS_Y 2 #define TS_Z 32 #define MAX_NUM_GPU 4 int num_devices = 0; static float *in_d[MAX_NUM_GPU]; static float *fil_d[MAX_NUM_GPU]; static float *out_d[MAX_NUM_GPU]; static int Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU]; inline void cuda_conv(); inline void cuda_conv_init(int,int); inline void cuda_conv_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(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; //__shared__ float s_filter[K*C*R*S]; //for (int i =0; i= K || oh>= OH || ow >= OW) return; int CHW = C*H*W; int CRS = C*R*S; int HW = H*W; int RS = R*S; if (C%8==0 && R==16 && S==16 && dilation==1 && pad==0 && stride==1) { for(int n = 0; n < N; n++) { float o = 0.f; //for (int c = 0; c < C; c+8) //for (int c = 0; c < C; c++) //for (int c = 0; c < C; c=c+2) for (int c = 0; c < C; c=c+4) { for (int r = 0; r < R; r++) { for (int s = 0; s < S; s++) { int h = oh + r ; int w = ow + s ; //if (h < 0 || h >= H || w < 0 || w >= W) continue; //float i = input[n * C * H * W + c * H * W + (oh+r) * W + ow+s]; //float f = filter[k * C * R * S + c * R * S + r * S + s]; //o += i * f; //float i0 = input[n * C * H * W + c * H * W + h * W + w]; //float f0 = filter[k * C * R * S + c * R * S + r * S + s]; //float i1 = input[n * C * H * W + (c+1) * H * W + h * W + w]; //float f1 = filter[k * C * R * S + (c+1) * R * S + r * S + s]; //o += i0*f0 + i1*f1; //float i0 = input[n * C * H * W + c * H * W + h * W + w]; //float f0 = filter[k * C * R * S + c * R * S + r * S + s]; //float i1 = input[n * C * H * W + (c+1) * H * W + h * W + w]; //float f1 = filter[k * C * R * S + (c+1) * R * S + r * S + s]; //o += i0*f0 + i1*f1; float i0 = input[n * CHW + c * HW + h * W + w]; float f0 = filter[k * CRS + c * RS + r * S + s]; float i1 = input[n * CHW + (c+1) * HW + h * W + w]; float f1 = filter[k * CRS + (c+1) * RS + r * S + s]; float i2 = input[n * CHW + (c+2) * HW + h * W + w]; float f2 = filter[k * CRS + (c+2) * RS + r * S + s]; float i3 = input[n * CHW + (c+3) * HW + h * W + w]; float f3 = filter[k * CRS + (c+3) * RS + r * S + s]; o += i0*f0 + i1*f1 + i2*f2 + i3*f3 ; //float i0 = input[n * C * H * W + c * H * W + h * W + w]; //float f0 = filter[k * C * R * S + c * R * S + r * S + s]; //float i1 = input[n * C * H * W + (c+1) * H * W + h * W + w]; //float f1 = filter[k * C * R * S + (c+1) * R * S + r * S + s]; //float i2 = input[n * C * H * W + (c+2) * H * W + h * W + w]; //float f2 = filter[k * C * R * S + (c+2) * R * S + r * S + s]; //float i3 = input[n * C * H * W + (c+3) * H * W + h * W + w]; //float f3 = filter[k * C * R * S + (c+3) * R * S + r * S + s]; //float i4 = input[n * C * H * W + (c+4) * H * W + h * W + w]; //float f4 = filter[k * C * R * S + (c+4) * R * S + r * S + s]; //float i5 = input[n * C * H * W + (c+5) * H * W + h * W + w]; //float f5 = filter[k * C * R * S + (c+5) * R * S + r * S + s]; //float i6 = input[n * C * H * W + (c+6) * H * W + h * W + w]; //float f6 = filter[k * C * R * S + (c+6) * R * S + r * S + s]; //float i7 = input[n * C * H * W + (c+7) * H * W + h * W + w]; //float f7 = filter[k * C * R * S + (c+7) * R * S + r * S + s]; //o += i0*f0 + i1*f1 + i2*f2 + i3*f3 + i4*f4 + i5*f5 + i6*f6 + i7*f7; } } } output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o; } } else { for(int n = 0; n < N; n++) { float o = 0.f; //for (int c = 0; c < C; c+8) 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; //float i0 = input[n * C * H * W + c * H * W + h * W + w]; //float f0 = filter[k * C * R * S + c * R * S + r * S + s]; //float i1 = input[n * C * H * W + (c+1) * H * W + h * W + w]; //float f1 = filter[k * C * R * S + (c+1) * R * S + r * S + s]; //float i2 = input[n * C * H * W + (c+2) * H * W + h * W + w]; //float f2 = filter[k * C * R * S + (c+2) * R * S + r * S + s]; //float i3 = input[n * C * H * W + (c+3) * H * W + h * W + w]; //float f3 = filter[k * C * R * S + (c+3) * R * S + r * S + s]; //float i4 = input[n * C * H * W + (c+4) * H * W + h * W + w]; //float f4 = filter[k * C * R * S + (c+4) * R * S + r * S + s]; //float i5 = input[n * C * H * W + (c+5) * H * W + h * W + w]; //float f5 = filter[k * C * R * S + (c+5) * R * S + r * S + s]; //float i6 = input[n * C * H * W + (c+6) * H * W + h * W + w]; //float f6 = filter[k * C * R * S + (c+6) * R * S + r * S + s]; //float i7 = input[n * C * H * W + (c+7) * H * W + h * W + w]; //float f7 = filter[k * C * R * S + (c+7) * R * S + r * S + s]; //o += i0*f0 + i1*f1 + i2*f2 + i3*f3 + i4*f4 + i5*f5 + i6*f6 + i7*f7; } } } output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o; } } } inline void cuda_conv() { // 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<<>>(in_d[i], fil_d[i], out_d[i], Nend[i] - Nbegin[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() ); } } inline void cuda_conv_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++) { Nbegin[i] = (p_n / num_devices) * i; Nend[i] = (p_n / num_devices) * (i + 1); } Nend[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(&in_d[i], (Nend[i] - Nbegin[i]) * C * H * W * sizeof(float)) ); CUDA_CALL( cudaMalloc(&fil_d[i], K * C * R * S * sizeof(float)) ); CUDA_CALL( cudaMalloc(&out_d[i], (Nend[i] - Nbegin[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(in_d[i], input + Nbegin[i] * C * H * W, (Nend[i] - Nbegin[i]) * C * H * W * sizeof(float), cudaMemcpyHostToDevice) ); CUDA_CALL( cudaMemcpy(fil_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() ); } } inline void cuda_conv_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 + Nbegin[i] * K * OH * OW, out_d[i], (Nend[i] - Nbegin[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) { if ( mpi_world_size == 2) { offset += p_n; MPI_Isend(&offset, 1, MPI_INT, 1 , 1, MPI_COMM_WORLD,&request); MPI_Isend(&p_n, 1, MPI_INT, 1 , 1, MPI_COMM_WORLD,&request); MPI_Isend(&input[offset*C*H*W], p_n*C*H*W, MPI_FLOAT, 1 , 1, MPI_COMM_WORLD,&request); MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 1, MPI_COMM_WORLD,&request); } p_n += remain; cuda_conv_init(); cuda_conv(); cuda_conv_final(); if ( mpi_world_size ==2) { MPI_Recv(&offset, 1, MPI_INT, 1, 2, MPI_COMM_WORLD, &status); MPI_Recv(&p_n, 1, MPI_INT, 1, 2, MPI_COMM_WORLD, &status); MPI_Recv(&output[offset*K*OH*OW], p_n*K*OH*OW, MPI_FLOAT, 1, 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); cuda_conv_init(); cuda_conv(); cuda_conv_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) { }