343 lines
12 KiB
Plaintext
343 lines
12 KiB
Plaintext
#include "convolution.h"
|
|
#include "util.h"
|
|
#include <mpi.h>
|
|
#include <stdio.h>
|
|
#include <omp.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); \
|
|
} \
|
|
}
|
|
|
|
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 thread_num = 20;
|
|
|
|
#define MAX_NUM_GPU 4
|
|
int num_devices = 0;
|
|
int num_devices_change = 4;
|
|
|
|
// 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 Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU];
|
|
|
|
#define MASTER_TO_SLAVE_TAG 1 //tag for messages sent from master to slaves
|
|
#define SLAVE_TO_MASTER_TAG 4 //tag for messages sent from slaves to master
|
|
|
|
MPI_Request request;
|
|
MPI_Status status;
|
|
|
|
__global__ void sgemm_2(float *input2, float *filter2, float *output2,
|
|
int N, int C, int H, int W, int K, int R, int S, int pad, int dilation, int stride, int dev, int start_n_gpu, int end_n_gpu) {
|
|
int i = blockDim.x * blockIdx.x + threadIdx.x; // raw index
|
|
int j = blockDim.y * blockIdx.y + threadIdx.y; // column index
|
|
|
|
//if (i >= N || j >= W) return;
|
|
if(i > (end_n_gpu-start_n_gpu)) return;
|
|
//if(i<start_m || i>=end_m) return;
|
|
|
|
int OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
int OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
|
|
//printf("Dev(%d)(%d/%d) StartN(%d), EndN(%d)\n", dev, i, j, start_n_gpu, end_n_gpu);
|
|
/* int preval1_1= C * H * W;
|
|
int preval1_2 = H * W;
|
|
|
|
int preval2_1 = C * R * S;
|
|
int preval2_2 = R * S;
|
|
|
|
int preval3_1 = K * OH * OW;
|
|
int preval3_2 = OH * OW;
|
|
*/
|
|
|
|
int v;
|
|
for (int n = start_n_gpu; n < end_n_gpu; ++n) {
|
|
v = (n - start_n_gpu);
|
|
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 = oh * stride - pad + r * dilation;
|
|
int w = ow * stride - pad + s * dilation;
|
|
if (h < 0 || h >= H || w < 0 || w >= W) continue;
|
|
//float i = input2[n * C * H * W + c * H * W + h * W + w];
|
|
float i = input2[v * C * H * W + c * H * W + h * W + w];
|
|
float f = filter2[k * C * R * S + c * R * S + r * S + s];
|
|
o += i * f;
|
|
}
|
|
}
|
|
}
|
|
|
|
//output2[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
|
|
output2[v * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//printf("Dev(%d)(%d/%d) StartN(%d), EndN(%d), BDx(%d), BDy(%d), Bx(%d), By(%d), Tx(%d), Ty(%d) \n", dev, i, j, start_n_gpu, end_n_gpu ,
|
|
// blockDim.x, blockDim.y, blockIdx.x, blockIdx.y, threadIdx.x, threadIdx.y);
|
|
}
|
|
|
|
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;
|
|
|
|
int size_n;
|
|
int start_n;
|
|
int end_n;
|
|
|
|
int start_n_no0;
|
|
int end_n_no0;
|
|
|
|
if(mpi_rank == 0){
|
|
|
|
for(int node=0; node< mpi_world_size; node++){
|
|
|
|
size_n = N / (mpi_world_size);
|
|
start_n = (node)*size_n;
|
|
|
|
if(node == 0) start_n_no0 = start_n;
|
|
|
|
if(((node+1) == mpi_world_size) && ((N%(mpi_world_size)) != 0)){
|
|
end_n = N;
|
|
if(node == 0) end_n_no0 = end_n;
|
|
}
|
|
else{
|
|
end_n = start_n + size_n;
|
|
if(node == 0) end_n_no0 = end_n;
|
|
}
|
|
|
|
if(node >= 1){
|
|
MPI_Send(&start_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD);
|
|
//printf("SEND start_n (%d) to (%d)\n", start_n, node);
|
|
|
|
MPI_Send(&end_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD);
|
|
//printf("SEND end_n (%d) to (%d)\n", end_n, node);
|
|
|
|
int sizeBuf = (end_n - start_n) * C * H * W;
|
|
//printf("SEND input Buff Len : (%d) to (%d)\n", sizeBuf, node);
|
|
MPI_Send(&input[start_n*C * H * W], sizeBuf, MPI_FLOAT, node, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD);
|
|
//printf("SEND input size (%d) to (%d)\n", sizeBuf, node);
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
alloc_tensor(&input, N, C, H, W);
|
|
|
|
MPI_Recv(&start_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD, &status);
|
|
//printf("RECV start_n (%d) at (%d)\n", start_n, mpi_rank);
|
|
|
|
MPI_Recv(&end_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD, &status);
|
|
//printf("RECV end_n (%d) at (%d)\n", end_n, mpi_rank);
|
|
|
|
int sizeBuf = (end_n - start_n) * C * H * W;
|
|
//printf("RECV input Buff Len : %d \n", sizeBuf);
|
|
MPI_Recv(&input[start_n*C * H * W], sizeBuf, MPI_FLOAT, 0, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD, &status);
|
|
//printf("RECV input size (%d) at (%d)\n", sizeBuf, mpi_rank);
|
|
}
|
|
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
|
|
if(mpi_rank > 0){
|
|
alloc_tensor(&filter, K, C, R, S);
|
|
}
|
|
MPI_Bcast(&filter[0], K*C*R*S, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
|
printf("Bcast filter (%d) to (%d) \n", K*C*R*S, mpi_rank);
|
|
|
|
if(mpi_rank > 0){
|
|
alloc_tensor(&output, N, K, OH, OW);
|
|
}
|
|
|
|
MPI_Bcast(&output[0], N*K*OH*OW, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
|
printf("Bcast output (%d) to (%d) \n", N*K*OH*OW, mpi_rank);
|
|
|
|
|
|
MPI_Barrier(MPI_COMM_WORLD);
|
|
|
|
if(mpi_rank == 0){
|
|
|
|
int size_n_gpu = end_n_no0 - start_n_no0;
|
|
//int Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU];
|
|
|
|
// N Devide with GPU
|
|
// Setup problem size for each GPU
|
|
for (int i = 0; i < num_devices; i++) {
|
|
Nbegin[i] = (size_n_gpu / num_devices) * i;
|
|
Nend[i] = (size_n_gpu / num_devices) * (i + 1);
|
|
}
|
|
int mod = (size_n_gpu % num_devices);
|
|
int small_size = size_n_gpu / num_devices;
|
|
Nend[num_devices - 1] = small_size * num_devices + mod;
|
|
|
|
for (int i = 0; i < num_devices; i++) {
|
|
printf("StartN(%d), EndN(%d)\n", Nbegin[i], Nend[i]);
|
|
}
|
|
|
|
// Allocate device memory for each GPU
|
|
for (int i = 0; i < num_devices; i++) {
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
CUDA_CALL( cudaMalloc(&input_d[i], (Nend[i] - Nbegin[i]) * C * H * W * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) );
|
|
CUDA_CALL( cudaMalloc(&output_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(input_d[i], input + Nbegin[i] * C * H * W ,
|
|
(Nend[i] - Nbegin[i]) * C * H * W * sizeof(float),
|
|
cudaMemcpyHostToDevice) );
|
|
CUDA_CALL( cudaMemcpy(filter_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( cudaDeviceSynchronize() );
|
|
}
|
|
|
|
//convolution_micro_gpu(input, output, filter, N, C, H, W, K, R, S, pad, dilation, stride, start_n_no0, end_n_no0);
|
|
|
|
printf("Start GPU Cal\n");
|
|
for (int i = 0; i < num_devices; i++) {
|
|
printf("StartN(%d), EndN(%d)\n", Nbegin[i], Nend[i]);
|
|
}
|
|
|
|
// Launch kernel on every GPU
|
|
for (int i = 0; i < num_devices; i++) {
|
|
//dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
|
|
//dim3 blockDim(1, 1, 1); // block : 1 x 32
|
|
dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
|
|
dim3 blockDim(1, 1, 1); // block : 1 x 32
|
|
|
|
printf("Start %d , %d \n", (Nend[i] - Nbegin[i]), W);
|
|
CUDA_CALL( cudaSetDevice(i) );
|
|
sgemm_2<<<gridDim, blockDim>>>(input_d[i], filter_d[i], output_d[i], N, C, H, W, K, R, S, pad, dilation, stride, i, Nbegin[i], Nend[i]);
|
|
}
|
|
|
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
|
for (int i = 0; i < num_devices; i++) {
|
|
CUDA_CALL( cudaDeviceSynchronize() );
|
|
}
|
|
printf("End GPU Cal\n");
|
|
|
|
// integrate ouput
|
|
// Download C matrix from GPUs
|
|
for (int i = 0; i < num_devices; i++) {
|
|
CUDA_CALL( cudaMemcpy(output + Nbegin[i] * K * OH * OW , output_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( cudaDeviceSynchronize() );
|
|
}
|
|
}
|
|
else{
|
|
|
|
//convolution_micro(input, output, filter, N, C, H, W, K, R, S, pad, dilation, stride, start_n, end_n);
|
|
// integrate ouput
|
|
|
|
|
|
if(mpi_rank >= 1){
|
|
MPI_Send(&start_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD);
|
|
//printf("SEND output start_n (%d) to (%d)\n", start_n, 0);
|
|
|
|
MPI_Send(&end_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD);
|
|
//printf("SEND output end_n (%d) to (%d)\n", end_n, 0);
|
|
|
|
int sizeBuf = (end_n - start_n) * K*OH*OW;
|
|
//printf("SEND output Buff Len : (%d) to (%d)\n", sizeBuf, 0);
|
|
MPI_Send(&output[start_n*K*OH*OW], sizeBuf, MPI_FLOAT, 0, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD);
|
|
//printf("SEND output size (%d) to (%d)\n", sizeBuf, 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(mpi_rank == 0){
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
for (int node = 1; node < mpi_world_size; node++) {// untill all slaves have handed back the processed data
|
|
//receive low bound from a slave
|
|
MPI_Recv(&start_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD, &status);
|
|
//receive upper bound from a slave
|
|
MPI_Recv(&end_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD, &status);
|
|
//receive processed data from a slave
|
|
MPI_Recv(&output[start_n*K*OH*OW], (end_n - start_n) * K*OH*OW, MPI_FLOAT, node, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD, &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);
|
|
|
|
|
|
// cuda call init
|
|
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
|
|
|
|
printf("Using %d devices\n", num_devices);
|
|
printf("Change Device Num (%d)->(%d)\n", num_devices, num_devices_change);
|
|
num_devices = num_devices_change;
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
void convolution_final(
|
|
int _N, int _C, int _H, int _W,
|
|
int _K, int _R, int _S,
|
|
int _pad, int _dilation, int _stride) {
|
|
}
|