368 lines
12 KiB
Plaintext
368 lines
12 KiB
Plaintext
#include "convolution.h"
|
|
#include <mpi.h>
|
|
#include <stdio.h>
|
|
#include <cstdio>
|
|
#include <cuda_runtime.h>
|
|
|
|
|
|
#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 1
|
|
int num_devices[2] = {0,0};
|
|
|
|
|
|
__global__ void conv(float *input, float *filter, float *output, int N, int C, int H, int W, int K, int R, int S, int pad, int dilation, int stride) {
|
|
|
|
int OH = (H + 2 * pad - dilation * (R-1) - 1) / stride + 1;
|
|
int OW = (W + 2 * pad - dilation * (S-1) - 1) / stride + 1;
|
|
|
|
int n = blockIdx.x;
|
|
int k = blockIdx.y;
|
|
int oh = blockIdx.z;
|
|
int ow = threadIdx.x;
|
|
|
|
if(n >= N || k >= K || oh >= OH || ow >= OW ) return;
|
|
|
|
// __syncthreads();
|
|
float tmp = 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 = 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];
|
|
tmp += i * f;
|
|
}
|
|
}
|
|
}
|
|
// __syncthreads();
|
|
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = tmp;
|
|
|
|
}
|
|
|
|
// Array of device (GPU) pointers
|
|
static float *a1_d[MAX_NUM_GPU];
|
|
static float *a2_d[MAX_NUM_GPU];
|
|
static float *b1_d[MAX_NUM_GPU];
|
|
static float *b2_d[MAX_NUM_GPU];
|
|
static float *c1_d[MAX_NUM_GPU];
|
|
static float *c2_d[MAX_NUM_GPU];
|
|
static int Mbegin1[MAX_NUM_GPU], Mend1[MAX_NUM_GPU];
|
|
static int Mbegin2[MAX_NUM_GPU], Mend2[MAX_NUM_GPU];
|
|
|
|
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 portion, low_bound, upper_bound;
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
|
|
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;
|
|
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);
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
size_t bytes_nchw = N*C*H*W*sizeof(float);
|
|
size_t bytes_kcrs = K*C*R*S*sizeof(float);
|
|
|
|
if (mpi_rank == 0) {
|
|
portion = (N / mpi_world_size);
|
|
for(int i=1; i < mpi_world_size; i++){
|
|
low_bound = i*portion;
|
|
if( ((i+1)==mpi_world_size) && (( N % mpi_world_size) != 0 ) ){
|
|
upper_bound = N;
|
|
}
|
|
else{
|
|
upper_bound = low_bound + portion;
|
|
}
|
|
|
|
MPI_Isend(&low_bound, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&upper_bound, 1, MPI_INT, i, 2, MPI_COMM_WORLD, &request);
|
|
|
|
MPI_Isend(&input[low_bound*C*H*W], ( (upper_bound - low_bound)*C*H*W ), MPI_FLOAT, i, 3, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&filter[0], (K*C*R*S), MPI_FLOAT, i, 4, MPI_COMM_WORLD, &request);
|
|
|
|
}
|
|
low_bound = 0;
|
|
upper_bound = portion;
|
|
|
|
int n = ((upper_bound-low_bound)/TS)*TS;
|
|
|
|
// Setup problem size for each GPU
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
Mbegin1[i] = (n / num_devices[0]) * i + low_bound;
|
|
Mend1[i] = (n / num_devices[0]) * (i + 1) + low_bound;
|
|
}
|
|
Mend1[num_devices[0] - 1] = n + low_bound;
|
|
|
|
// Upload A and B matrix to every GPU
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaMemcpy(a1_d[i], input + Mbegin1[i] * C * H * W ,
|
|
(Mend1[i] - Mbegin1[i]) * C * H * W * sizeof(float),
|
|
cudaMemcpyHostToDevice) );
|
|
CUDA_CALL( cudaMemcpy(b1_d[i], filter, K * C * R * S * sizeof(float), cudaMemcpyHostToDevice) );
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
// Launch kernel on every GPU
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
dim3 gridDim( Mend1[i] - Mbegin1[i] , K , OH);
|
|
dim3 blockDim(OW, 1);
|
|
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
conv<<<gridDim, blockDim>>>(a1_d[i], b1_d[i], c1_d[i], Mend1[i]-Mbegin1[i], C, H, W , K , R ,S ,pad,dilation, stride );
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
} // mpi_rank == 0
|
|
else{
|
|
|
|
MPI_Recv(&low_bound, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&upper_bound, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, &status);
|
|
input = (float*)malloc(bytes_nchw);
|
|
filter = (float*)malloc(bytes_kcrs);
|
|
MPI_Recv(&input[low_bound*C*H*W], ( (upper_bound - low_bound)*C*H*W ), MPI_FLOAT, 0, 3, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&filter[0], (K*C*R*S ), MPI_FLOAT, 0, 4, MPI_COMM_WORLD, &status);
|
|
|
|
int n2 = ((upper_bound-low_bound)/TS)*TS;
|
|
|
|
// Setup problem size for each GPU
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
Mbegin2[i] = (n2 / num_devices[1]) * i;
|
|
Mend2[i] = (n2 / num_devices[1]) * (i + 1);
|
|
}
|
|
Mend2[num_devices[1] - 1] = n2;
|
|
//
|
|
// Upload A and B matrix to every GPU
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaMemcpy(a2_d[i], input + (Mbegin2[i] + low_bound )* C * H * W ,
|
|
(Mend2[i] - Mbegin2[i]) * C * H * W * sizeof(float),
|
|
cudaMemcpyHostToDevice) );
|
|
CUDA_CALL( cudaMemcpy(b2_d[i], filter, K * C * R * S * sizeof(float), cudaMemcpyHostToDevice) );
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
// Launch kernel on every GPU
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
dim3 gridDim( Mend2[i] - Mbegin2[i] , K , OH);
|
|
dim3 blockDim(OW, 1);
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
conv<<<gridDim, blockDim>>>(a2_d[i], b2_d[i], c2_d[i], Mend2[i]-Mbegin2[i], C, H, W , K , R ,S,pad,dilation, stride );
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
free(input);
|
|
free(filter);
|
|
|
|
}
|
|
}
|
|
//
|
|
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;
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
size_t bytes_nchw = N*C*H*W*sizeof(float);
|
|
size_t bytes_kcrs = K*C*R*S*sizeof(float);
|
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
|
|
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);
|
|
|
|
if (mpi_rank == 0) {
|
|
portion = (N / mpi_world_size);
|
|
for(int i=1; i < mpi_world_size; i++){
|
|
low_bound = i*portion;
|
|
if( ((i+1)==mpi_world_size) && (( N % mpi_world_size) != 0 ) ){
|
|
upper_bound = N;
|
|
}
|
|
else{
|
|
upper_bound = low_bound + portion;
|
|
}
|
|
|
|
MPI_Isend(&low_bound, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&upper_bound, 1, MPI_INT, i, 2, MPI_COMM_WORLD, &request);
|
|
}
|
|
low_bound = 0;
|
|
upper_bound = portion;
|
|
|
|
int n = ((upper_bound-low_bound)/TS)*TS;
|
|
CUDA_CALL( cudaGetDeviceCount(&num_devices[0]) );
|
|
|
|
printf("1:Using %d devices\n", num_devices[0]);
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
cudaDeviceProp prop;
|
|
CUDA_CALL( cudaGetDeviceProperties(&prop, i) );
|
|
|
|
// Try printing more detailed information here
|
|
printf("[1:GPU %d] %s\n", i, prop.name);
|
|
}
|
|
|
|
if (num_devices[0] <= 0) {
|
|
printf("No CUDA device found. Aborting\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Setup problem size for each GPU
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
Mbegin1[i] = (n / num_devices[0]) * i + low_bound;
|
|
Mend1[i] = (n / num_devices[0]) * (i + 1) + low_bound;
|
|
}
|
|
Mend1[num_devices[0] - 1] = n + low_bound;
|
|
|
|
// Allocate device memory for each GPU
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaMalloc(&a1_d[i], (Mend1[i] - Mbegin1[i]) * C * H * W * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&b1_d[i], K * C * R * S * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&c1_d[i], (Mend1[i] - Mbegin1[i]) * K * OH * OW * sizeof(float)) );
|
|
}
|
|
//
|
|
}
|
|
else{
|
|
|
|
MPI_Recv(&low_bound, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&upper_bound, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, &status);
|
|
//
|
|
int n2 = ((upper_bound-low_bound)/TS)*TS;
|
|
CUDA_CALL( cudaGetDeviceCount(&num_devices[1]) );
|
|
|
|
printf("2:Using %d devices\n", num_devices[1]);
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
cudaDeviceProp prop;
|
|
CUDA_CALL( cudaGetDeviceProperties(&prop, i) );
|
|
|
|
printf("[2:GPU %d] %s\n", i, prop.name);
|
|
}
|
|
|
|
if (num_devices[1] <= 0) {
|
|
printf("No CUDA device found. Aborting\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Setup problem size for each GPU
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
Mbegin2[i] = (n2 / num_devices[1]) * i;
|
|
Mend2[i] = (n2 / num_devices[1]) * (i + 1);
|
|
}
|
|
Mend2[num_devices[1] - 1] = n2;
|
|
|
|
// Allocate device memory for each GPU
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaMalloc(&a2_d[i], (Mend2[i] - Mbegin2[i]) * C * H * W * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&b2_d[i], K * C * R * S * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&c2_d[i], (Mend2[i] - Mbegin2[i]) * K * OH * OW * sizeof(float)) );
|
|
}
|
|
//
|
|
} // else
|
|
}
|
|
//
|
|
void convolution_final(
|
|
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;
|
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
|
|
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
size_t bytes_nkohow = N*K*OH*OW*sizeof(float);
|
|
|
|
if (mpi_rank == 0) {
|
|
|
|
for(int i = 1; i < mpi_world_size; i++){
|
|
|
|
MPI_Recv(&low_bound, 1, MPI_INT, i, 4, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&upper_bound, 1, MPI_INT, i, 5, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&output[low_bound*K*OH*OW], ( (upper_bound - low_bound)*K*OH*OW ), MPI_FLOAT, i, 6, MPI_COMM_WORLD, &status);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaMemcpy( output + Mbegin1[i] * K * OH * OW , c1_d[i],
|
|
(Mend1[i] - Mbegin1[i]) * K * OH * OW * sizeof(float),
|
|
cudaMemcpyDeviceToHost) );
|
|
}
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[0]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
}
|
|
else{
|
|
|
|
output = (float*)malloc(bytes_nkohow);
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaMemcpy( output + (Mbegin2[i] + low_bound) * K * OH * OW , c2_d[i],
|
|
(Mend2[i] - Mbegin2[i]) * K * OH * OW * sizeof(float),
|
|
cudaMemcpyDeviceToHost) );
|
|
}
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices[1]; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
MPI_Isend(&low_bound, 1, MPI_INT, 0, 4, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&upper_bound, 1, MPI_INT, 0, 5, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&output[low_bound*K*OH*OW], ( (upper_bound - low_bound)*K*OH*OW ), MPI_FLOAT, 0, 6, MPI_COMM_WORLD, &request);
|
|
free(output);
|
|
}
|
|
|
|
}
|