#include "convolution.h" #include "util.h" #include #include #include #include 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; static int input_line; static int filter_size; static int output_line; #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 int num_devices = 0; // Array of device (GPU) pointers static float *i_d[MAX_NUM_GPU]; static float *f_d[MAX_NUM_GPU]; static float *o_d[MAX_NUM_GPU]; static int Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU]; __global__ void conv4(float *input, float* filter, float* output, int Nsplit, int C, int H, int W, int K, int R, int S, int OH, int OW, int pad, int dilation, int stride) { int n = blockIdx.x; int k = blockIdx.y; int oh = blockIdx.z; int ow = threadIdx.x; float o = 0.f; for (int c = 0; c < C; ++c) { // # of input channel for (int r = 0; r < R; ++r) { // filter height (=row) for (int s = 0; s < S; s+=4) { // filter width (=column) int h = oh * stride - pad + r * dilation; int w = ow * stride - pad + s * dilation; if (h < 0 || h >= H || w < 0 || w >= W) continue; int idx_i = (n * C * H * W) + (c * H * W) + (h * W) + w; int idx_f = (k * C * R * S) + (c * R * S) + (r * S) + s; float4 i = make_float4(input[idx_i + 0], input[idx_i + 1], input[idx_i + 2], input[idx_i + 3]); float4 f = reinterpret_cast(filter)[idx_f/4]; o = o + i.x * f.x + i.y * f.y + i.z * f.z + i.w * f.w ; } } } output[(n * K * OH * OW) + (k * OH * OW) + (oh * OW) + ow] = o; } __global__ void conv(float *input, float* filter, float* output, int Nsplit, int C, int H, int W, int K, int R, int S, int OH, int OW, int pad, int dilation, int stride) { int n = blockIdx.x; int k = blockIdx.y; int oh = blockIdx.z; int ow = threadIdx.x; float o = 0.f; for (int c = 0; c < C; ++c) { // # of input channel for (int r = 0; r < R; ++r) { // filter height (=row) for (int s = 0; s < S; ++s) { // filter width (=column) 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 cuda_init(int ns, int ne) { int N_ = ne - ns; 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] = ns + (N_ / num_devices) * i; Nend[i] = ns + (N_ / num_devices) * (i + 1); /* debug */// printf("%d: Nbegin[%d] = %d, Nend[%d] = %d\n", mpi_rank, i, Nbegin[i], i, Nend[i]); } Nend[num_devices - 1] = ns + N_; // Allocate device memory for each GPU for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaSetDevice(i) ); CUDA_CALL( cudaMalloc(&i_d[i], (Nend[i] - Nbegin[i]) * input_line * sizeof(float)) ); CUDA_CALL( cudaMalloc(&f_d[i], filter_size * sizeof(float)) ); CUDA_CALL( cudaMalloc(&o_d[i], (Nend[i] - Nbegin[i]) * output_line * sizeof(float)) ); } // Upload A and B matrix to every GPU for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaMemcpy(i_d[i], input + Nbegin[i] * input_line, (Nend[i] - Nbegin[i]) * input_line * sizeof(float), cudaMemcpyHostToDevice) ); CUDA_CALL( cudaMemcpy(f_d[i], filter, filter_size * sizeof(float), cudaMemcpyHostToDevice) ); } // DO NOT REMOVE; NEEDED FOR TIME NEASURE for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaDeviceSynchronize() ); } } void cuda_run() { // Launch kernel on every GPU for (int i = 0; i < num_devices; i++) { int Nsplit = Nend[i] - Nbegin[i]; dim3 blockDim(OW, 1); dim3 gridDim(Nsplit, K, OH); if (S==16 && R==16 && dilation==1) { CUDA_CALL( cudaSetDevice(i) ); conv4<<>>(i_d[i], f_d[i], o_d[i], Nsplit, C, H, W, K, R, S, OH, OW, pad, dilation, stride); } else { // printf("\nconv4!\n"); CUDA_CALL( cudaSetDevice(i) ); conv<<>>(i_d[i], f_d[i], o_d[i], Nsplit, C, H, W, K, R, S, OH, OW, pad, dilation, stride); } } // DO NOT REMOVE; NEEDED FOR TIME NEASURE for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaDeviceSynchronize() ); } } void cuda_final() { // Download C matrix from GPUs for (int i = 0; i < num_devices; i++) { CUDA_CALL( cudaMemcpy(output + Nbegin[i] * output_line, o_d[i], (Nend[i] - Nbegin[i]) * output_line * sizeof(float), cudaMemcpyDeviceToHost) ); } // DO NOT REMOVE; NEEDED FOR TIME MEASURE for (int i = 0; i < num_devices; 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; OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1; OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1; input_line = C*H*W; filter_size = K*C*R*S; output_line = K*OH*OW; // Allocate if(mpi_rank != 0){ alloc_tensor(&input, N, C, H, W); alloc_tensor(&output, N, K, OH, OW); alloc_tensor(&filter, K, C, R, S); } // N split int ns[mpi_world_size], ne[mpi_world_size]; for (int i = 0; i < mpi_world_size; i++) { ns[i] = N / mpi_world_size * i; ne[i] = N / mpi_world_size * (i + 1); } ne[mpi_world_size - 1] = N; // Scatter Input if (mpi_rank == 0) { for (int i = 1; i < mpi_world_size; i++) { MPI_Send(input + ns[i] * input_line, (ne[i] - ns[i]) * input_line, MPI_FLOAT, i, 0, MPI_COMM_WORLD); } } else { MPI_Recv(input + ns[mpi_rank] * input_line, (ne[mpi_rank] - ns[mpi_rank]) * input_line, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, nullptr); } // Broadcast Filter MPI_Bcast(filter, filter_size, MPI_FLOAT, 0, MPI_COMM_WORLD); cuda_init(ns[mpi_rank], ne[mpi_rank]); cuda_run(); cuda_final(); // Gather Output if (mpi_rank == 0) { for (int i = 1; i < mpi_world_size; i++) { MPI_Recv(output + ns[i] * output_line, (ne[i] - ns[i]) * output_line, MPI_FLOAT, i, 0, MPI_COMM_WORLD, nullptr); } } else { MPI_Send(output + ns[mpi_rank] * output_line, (ne[mpi_rank] - ns[mpi_rank]) * output_line, MPI_FLOAT, 0, 0, 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) { }