272 lines
7.5 KiB
Plaintext
272 lines
7.5 KiB
Plaintext
#include "convolution.h"
|
|
#include <mpi.h>
|
|
#include <stdio.h>
|
|
#include "util.h"
|
|
#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 NODE 2
|
|
#define MAX_NUM_GPU 4
|
|
|
|
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 num_devices = 0;
|
|
|
|
|
|
|
|
__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 m = blockDim.x * blockIdx.x + threadIdx.x;
|
|
int j = blockDim.y * blockIdx.y + threadIdx.y;
|
|
int k = blockDim.z * blockIdx.z + threadIdx.z;
|
|
|
|
//int m = threadIdx.x;
|
|
//int j = threadIdx.y;
|
|
//int k = threadIdx.z;
|
|
|
|
//printf("kernel test %d %d %d\n", blockDim.x, blockIdx.x, threadIdx.x);
|
|
|
|
|
|
if (m >= K || j>= OH || k >= OW) return;
|
|
|
|
for(int n=0;n<N;n++){
|
|
//for (int k = 0; k < K; ++k) {
|
|
//for (int oh = 0; oh < OH; ++oh) {
|
|
//for (int ow = 0; ow < OW; ++ow) {
|
|
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 = j * stride - pad + r * dilation;
|
|
int w = k * 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[m * C * R * S + c * R * S + r * S + s];
|
|
o += i * f;
|
|
}
|
|
}
|
|
}
|
|
output[n * K * OH * OW + m * OH * OW + j * OW + k] = o;
|
|
//printf("kernel test = %f ",o);
|
|
//}
|
|
//printf("\n");
|
|
//}
|
|
//}
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
MPI_Status status;
|
|
MPI_Request request1,request2;
|
|
|
|
int rows;
|
|
int half = N/mpi_world_size;
|
|
if(mpi_world_size == 2){
|
|
rows = N-half;
|
|
}else{
|
|
rows = N;
|
|
}
|
|
|
|
int offset = N/mpi_world_size;
|
|
|
|
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 != 1){
|
|
MPI_Isend(&input[(offset)*C*H*W], (rows)*C*H*W, MPI_FLOAT, 1 , 1, MPI_COMM_WORLD,&request1);
|
|
MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 1, MPI_COMM_WORLD,&request2);
|
|
}
|
|
|
|
//printf("\n test %d %d\n",rows,offset);
|
|
convolution_cuda_init(rows,0);
|
|
convolution_cuda();
|
|
convolution_cuda_final(0);
|
|
|
|
if(mpi_world_size != 1){
|
|
MPI_Recv(&output[(offset)*K*OH*OW], (rows)*K*OH*OW, MPI_FLOAT, 1, 2, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
|
|
}else if(mpi_rank > 0){
|
|
alloc_tensor(&input, rows, C,H,W);
|
|
alloc_tensor(&filter, K,C,R,S);
|
|
alloc_tensor(&output, rows, K,OH,OW);
|
|
|
|
//printf("\n test11 %d %d %f\n",i,offset,*(input + (Mbegin[i]+offset) * C * H * W));
|
|
MPI_Recv(input, (rows)*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);
|
|
|
|
//printf("\n test %d %d\n",rows,offset);
|
|
convolution_cuda_init(rows,0);
|
|
convolution_cuda();
|
|
convolution_cuda_final(0);
|
|
|
|
MPI_Send(output, (rows)*K*OH*OW, MPI_FLOAT, 0, 2, MPI_COMM_WORLD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Array of device (GPU) pointers
|
|
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() {
|
|
|
|
// Launch kernel on every GPU
|
|
for (int i = 0; i < num_devices; i++) {
|
|
dim3 blockDim(4, 4, 64);
|
|
dim3 gridDim((K+3)/4, (OH+3)/4, (OW+63)/64);
|
|
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
conv_kernel<<<gridDim, blockDim>>>(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( cudaDeviceSynchronize() );
|
|
}
|
|
}
|
|
|
|
|
|
void convolution_cuda_init(int rows,int offset) {
|
|
|
|
//printf("\n test %d %d\n",rows,offset);
|
|
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] = (rows / num_devices) * i;
|
|
Mend[i] = (rows / num_devices) * (i + 1);
|
|
//printf("\n test %d %d\n",Mbegin[i],Mend[i]);
|
|
|
|
}
|
|
|
|
Mend[num_devices - 1] = rows;
|
|
|
|
// 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]+offset) * 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) );
|
|
//printf("\n test %d %d %f\n",i,offset,*(input + (Mbegin[i]+offset) * C * H * W));
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices; i++) {
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
}
|
|
|
|
void convolution_cuda_final(int offset) {
|
|
// 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]+offset) * 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( cudaDeviceSynchronize() );
|
|
}
|
|
}
|
|
|
|
|
|
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) {
|
|
}
|
|
|