cublas_beamformer.cu 17.2 KB
Newer Older
Mark Ruzindana's avatar
Mark Ruzindana committed
1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <curand.h>
#include <assert.h>
#include <unistd.h>
#include <cublas_v2.h>
#include <iostream>
#include <complex.h>
#include <math.h>
#include <cuComplex.h>
12
#include <cuda.h>
Mark Ruzindana's avatar
Mark Ruzindana committed
13 14 15 16 17
#include <cuda_runtime.h>
#include "cublas_beamformer.h"

using namespace std;

18
void beamform();
19

20 21
__global__
void transpose(signed char* data, signed char* tra_data);
22 23 24 25 26 27 28 29

__global__
void data_restructure(signed char * data, cuComplex * data_restruc);

__global__
void sti_reduction(cuComplex * data_in, float * data_out);


30 31 32 33 34
// Struct defintion for beamformer metadata
typedef struct bf_metadata_struct {
	float offsets[14];
	char cal_filename[65];
	char algorithm[65];
35
	char weight_filename[65];
36 37
	long long unsigned int xid;
} bf_metadata;
38

39 40
static bf_metadata my_metadata;

41

Mark Ruzindana's avatar
Mark Ruzindana committed
42
static cuComplex * d_weights = NULL;
43

Mark Ruzindana's avatar
Mark Ruzindana committed
44
void update_weights(char * filename){
45 46 47

	printf("RTBF: In update_weights()...\n");

Mark Ruzindana's avatar
Mark Ruzindana committed
48 49 50 51 52 53 54 55
	char weight_filename[128];
	strcpy(weight_filename, filename);
	FILE * weights;
	float * bf_weights;
	float complex * weights_dc;
	float complex * weights_dc_n;

	// Allocate heap memory for file data
56 57 58
	bf_weights = (float *)malloc(2*BN_WEIGHTS*sizeof(float));
	weights_dc = (float complex *)malloc(BN_WEIGHTS*sizeof(float complex *));
	weights_dc_n = (float complex *)malloc(BN_WEIGHTS*sizeof(float complex *));
59 60

	// open weight file
Mark Ruzindana's avatar
Mark Ruzindana committed
61 62 63 64
	weights = fopen(weight_filename, "r");

	int j;
	if (weights != NULL) {
65

66
		fread(bf_weights, sizeof(float), 2*BN_WEIGHTS, weights);
Mark Ruzindana's avatar
Mark Ruzindana committed
67

68 69 70 71 72
		fread(my_metadata.offsets, sizeof(float), 14, weights);
		fread(my_metadata.cal_filename, sizeof(char), 64, weights);
		fread(my_metadata.algorithm, sizeof(char), 64, weights);
		fread(&(my_metadata.xid), sizeof(long long unsigned int), 1, weights);

73 74 75 76
		my_metadata.cal_filename[64] = '\0';
		my_metadata.algorithm[64] = '\0';


77 78 79 80
		// Extract all path information from weight_filename for metadata
		char * short_filename = strrchr(weight_filename, '/');
		if (short_filename != NULL) {
			strcpy(my_metadata.weight_filename, short_filename+1);
81
		}
82 83 84 85
		else {
			strcpy(my_metadata.weight_filename, weight_filename);
		}

Mark Ruzindana's avatar
Mark Ruzindana committed
86
		// Convert to complex numbers (do a conjugate at the same time)
87
		for(j = 0; j < BN_WEIGHTS; j++){
Mark Ruzindana's avatar
Mark Ruzindana committed
88 89 90 91 92
			weights_dc_n[j] = bf_weights[2*j] - bf_weights[(2*j)+1]*I;
		}

		// Transpose the weights
		int m,n;
93
		float complex transpose[BN_BEAM][BN_ELE_BLOC*BN_BIN];
94
		for(m=0;m<BN_BEAM;m++){
95 96
			for(n=0;n<BN_ELE_BLOC*BN_BIN;n++){
				transpose[m][n] = weights_dc_n[m*BN_ELE_BLOC*BN_BIN + n];
Mark Ruzindana's avatar
Mark Ruzindana committed
97 98
			}
		}
99
		for(n=0;n<BN_ELE_BLOC*BN_BIN;n++){
100 101
			for(m=0;m<BN_BEAM;m++){
				weights_dc[n*BN_BEAM+ m] = transpose[m][n];
Mark Ruzindana's avatar
Mark Ruzindana committed
102 103 104 105
			}
		}
		fclose(weights);
	}
106

Mark Ruzindana's avatar
Mark Ruzindana committed
107
	// Copy weights to device
108
	cudaMemcpy(d_weights, weights_dc, BN_WEIGHTS*sizeof(cuComplex), cudaMemcpyHostToDevice); //r_weights instead of weights_dc //*BN_TIME
Mark Ruzindana's avatar
Mark Ruzindana committed
109

110
	// free memory
Mark Ruzindana's avatar
Mark Ruzindana committed
111
	free(weights_dc);
112 113 114 115
	free(weights_dc_n);
	free(bf_weights);

	return; 
Mark Ruzindana's avatar
Mark Ruzindana committed
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129
void bf_get_offsets(float * offsets){
	for(int i = 0; i<BN_BEAM; i++){
		offsets[i] = my_metadata.offsets[i];
	}
}

void bf_get_cal_filename(char * cal_filename){
	for(int i = 0; i< 65; i++){
		cal_filename[i] = my_metadata.cal_filename[i];
	}
}

130
void bf_get_algorithm(char * algorithm){
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	for(int i = 0; i< 65; i++){
		algorithm[i] = my_metadata.algorithm[i];
	}
}

void bf_get_weight_filename(char * weight_filename){
	int num_chars = strlen(my_metadata.weight_filename);
	for (int i = 0; i < num_chars; i++) {
		weight_filename[i] = my_metadata.weight_filename[i];
	}
	for (int i = num_chars; i < 64; i++) {
		weight_filename[i] = ' ';
	}
	weight_filename[64] = '\0';
}

long long unsigned int bf_get_xid(){
	return my_metadata.xid;
}

Mark Ruzindana's avatar
Mark Ruzindana committed
151 152
static cuComplex * d_beamformed = NULL;
static cuComplex * d_data = NULL;
153
static signed char * d_data1 = NULL; // Device memory for input data
154
static signed char * d_data2 = NULL;
Mark Ruzindana's avatar
Mark Ruzindana committed
155 156
static float * d_outputs;

157
static cublasHandle_t handle;
158 159 160
static cuComplex **d_arr_A = NULL;
static cuComplex **d_arr_B = NULL;
static cuComplex **d_arr_C = NULL;
Mark Ruzindana's avatar
Mark Ruzindana committed
161 162
void init_beamformer(){

163
	// Allocate memory for the weights, data, beamformer output, and sti output.
164
	cudaMalloc((void **)&d_weights, BN_WEIGHTS*sizeof(cuComplex)); //*BN_TIME
Mark Ruzindana's avatar
Mark Ruzindana committed
165

166
	cudaMalloc((void **)&d_data1, 2*BN_SAMP*sizeof(signed char));
Mark Ruzindana's avatar
Mark Ruzindana committed
167

168 169
	//cudaMalloc((void **)&d_data2, 2*BN_SAMP*sizeof(signed char));

170
	cudaMalloc((void **)&d_data, BN_SAMP*sizeof(cuComplex));
Mark Ruzindana's avatar
Mark Ruzindana committed
171

172
	cudaError_t err_malloc = cudaMalloc((void **)&d_beamformed, BN_TBF*sizeof(cuComplex));
Mark Ruzindana's avatar
Mark Ruzindana committed
173 174 175 176
	if (err_malloc != cudaSuccess) {
		printf("CUDA Error (cudaMalloc2): %s\n", cudaGetErrorString(err_malloc));
	}

177
	cudaMalloc((void **)&d_outputs, BN_POL*(BN_OUTPUTS*sizeof(float)/2));
Mark Ruzindana's avatar
Mark Ruzindana committed
178

179 180 181 182
    /**********************************************************
    * Create a handle for CUBLAS
    **********************************************************/
    cublasCreate(&handle);
Mark Ruzindana's avatar
Mark Ruzindana committed
183 184 185 186
	cudaError_t cudaStat;

	int nr_rows_A, nr_cols_A, nr_rows_B, nr_cols_B, nr_rows_C, nr_cols_C;

187 188 189 190 191 192
	nr_rows_A = BN_BEAM;
	nr_cols_A = BN_ELE_BLOC;
	nr_rows_B = BN_ELE_BLOC;
	nr_cols_B = BN_TIME;
	nr_rows_C = BN_BEAM;
	nr_cols_C = BN_TIME;
Mark Ruzindana's avatar
Mark Ruzindana committed
193

194 195 196 197 198
	// Allocate memory to host arrays - This is all memory allocated to arrays that are used by gemmBatched. Allocate 3 arrays on CPU
	const cuComplex **h_arr_A = 0;
	const cuComplex **h_arr_B = 0;
	cuComplex **h_arr_C = 0;

199 200 201
	h_arr_A = (const cuComplex **)malloc(nr_rows_A * nr_cols_A *BN_BIN*sizeof(const cuComplex*));
	h_arr_B = (const cuComplex **)malloc(nr_rows_B * nr_cols_B *BN_BIN*sizeof(const cuComplex*));
	h_arr_C = (cuComplex **)malloc(nr_rows_C * nr_cols_C *BN_BIN*sizeof(cuComplex*));
Mark Ruzindana's avatar
Mark Ruzindana committed
202 203

	// Allocate memory for each batch in an array.
204
	for(int i = 0; i < BN_BIN; i++){
Mark Ruzindana's avatar
Mark Ruzindana committed
205 206 207 208 209 210
		h_arr_A[i] = d_weights + i*nr_rows_A*nr_cols_A;
		h_arr_B[i] = d_data + i*nr_rows_B*nr_cols_B;
		h_arr_C[i] = d_beamformed + i*nr_rows_C*nr_cols_C;
	}

	// Allocate memory to arrays on device.
211
	cudaStat = cudaMalloc((void **)&d_arr_A,nr_rows_A * nr_cols_A * BN_BIN * sizeof(cuComplex*));
Mark Ruzindana's avatar
Mark Ruzindana committed
212
	assert(!cudaStat);
213
	cudaStat = cudaMalloc((void **)&d_arr_B,nr_rows_B * nr_cols_B * BN_BIN * sizeof(cuComplex*));
Mark Ruzindana's avatar
Mark Ruzindana committed
214
	assert(!cudaStat);
215
	cudaStat = cudaMalloc((void **)&d_arr_C,nr_rows_C * nr_cols_C * BN_BIN * sizeof(cuComplex*));
Mark Ruzindana's avatar
Mark Ruzindana committed
216 217 218
	assert(!cudaStat);

	// Copy memory from host to device.
219
	cudaStat = cudaMemcpy(d_arr_A,h_arr_A,nr_rows_A * nr_cols_A * BN_BIN * sizeof(cuComplex*),cudaMemcpyHostToDevice);
Mark Ruzindana's avatar
Mark Ruzindana committed
220
	assert(!cudaStat);
221
	cudaStat = cudaMemcpy(d_arr_B,h_arr_B,nr_rows_B * nr_cols_B * BN_BIN * sizeof(cuComplex*),cudaMemcpyHostToDevice);
Mark Ruzindana's avatar
Mark Ruzindana committed
222
	assert(!cudaStat);
223
	cudaStat = cudaMemcpy(d_arr_C,h_arr_C,nr_rows_C * nr_cols_C * BN_BIN * sizeof(cuComplex*),cudaMemcpyHostToDevice);
Mark Ruzindana's avatar
Mark Ruzindana committed
224 225
	assert(!cudaStat);

226 227 228
	free(h_arr_A);
	free(h_arr_B);
	free(h_arr_C);
Mark Ruzindana's avatar
Mark Ruzindana committed
229

230 231
	return;
        
Mark Ruzindana's avatar
Mark Ruzindana committed
232 233
}

234
signed char * data_in(char * input_filename){
Mark Ruzindana's avatar
Mark Ruzindana committed
235 236 237
	FILE * data;

	// File data pointers
238
	signed char * bf_data;
Mark Ruzindana's avatar
Mark Ruzindana committed
239 240

	// Complex data pointers
241
	// float complex * data_dc;
Mark Ruzindana's avatar
Mark Ruzindana committed
242 243

	// Allocate heap memory for file data
244 245
	bf_data = (signed char *)malloc(2*BN_SAMP*sizeof(signed char));
	//data_dc = (float complex *)malloc(BN_SAMP*sizeof(float complex *));
Mark Ruzindana's avatar
Mark Ruzindana committed
246 247 248 249 250 251 252 253

	// Open files
	data = fopen(input_filename, "r");

	/*********************************************************
	 * Read in Data
	 *********************************************************/
	if (data != NULL) {
254
		fread(bf_data, sizeof(signed char), 2*BN_SAMP, data);
255
		/*
Mark Ruzindana's avatar
Mark Ruzindana committed
256 257
		int j;
		// Make 'em complex!
258
		for (j = 0; j < BN_SAMP; j++) {
Mark Ruzindana's avatar
Mark Ruzindana committed
259 260
			data_dc[j] = bf_data[2*j] + bf_data[(2*j)+1]*I;
		}
261
		*/
Mark Ruzindana's avatar
Mark Ruzindana committed
262 263

		// Specify grid and block dimensions
264 265
		// dim3 dimBlock_d(BN_ELE, 1, 1);
		// dim3 dimGrid_d(BN_TIME, BN_BIN, 1);
Mark Ruzindana's avatar
Mark Ruzindana committed
266

267 268
		//cuComplex * d_data_in = d_data1;
		//cuComplex * d_data_out = d_data;
Mark Ruzindana's avatar
Mark Ruzindana committed
269

270
		//cudaMemcpy(d_data_in,    data_dc,   BN_SAMP*sizeof(cuComplex), cudaMemcpyHostToDevice);
Mark Ruzindana's avatar
Mark Ruzindana committed
271 272

		// Restructure data for cublasCgemmBatched function.
273
		//data_restructure<<<dimGrid_d, dimBlock_d>>>(d_data_in, d_data_out);
Mark Ruzindana's avatar
Mark Ruzindana committed
274 275 276

		fclose(data);
	}
277 278 279
	//free(bf_data);
	//free(data_dc);
	return bf_data;
Mark Ruzindana's avatar
Mark Ruzindana committed
280 281
}

282
void beamform() {
Mark Ruzindana's avatar
Mark Ruzindana committed
283
	int nr_rows_A, nr_cols_A, nr_rows_B, nr_cols_B, nr_rows_C;
284
	nr_rows_A = BN_BEAM;
285 286
	nr_cols_A = BN_ELE_BLOC;
	nr_rows_B = BN_ELE_BLOC;
287 288
	nr_cols_B = BN_TIME;
	nr_rows_C = BN_BEAM;
Mark Ruzindana's avatar
Mark Ruzindana committed
289 290

	// Leading dimensions are always the rows of each matrix since the data is stored in a column-wise order.
291
	int lda=nr_rows_A, ldb=nr_rows_B, ldc=nr_rows_C;
Mark Ruzindana's avatar
Mark Ruzindana committed
292 293 294 295 296 297 298 299
	cuComplex alf;
	cuComplex bet;

	alf.x = 1;
	alf.y = 0;
	bet.x = 0;
	bet.y = 0;

300
	int batchCount = BN_BIN; 				// There must be the same number of batches in each array.
Mark Ruzindana's avatar
Mark Ruzindana committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	cublasStatus_t stat;
	/*
		This function performs a matrix multiplication of the data and the weights.
		Weights - d_arr_A, Data - d_arr_B, and the output - d_arr_C.
	*/
	stat = cublasCgemmBatched(
			handle,							// handle to the cuBLAS library context.
			CUBLAS_OP_N,					// Operation on matrices within array A.
			CUBLAS_OP_N,					// Operation on matrices within array B.
			nr_rows_A,						// Number of rows in matrix A and C.
			nr_cols_B,						// Number of columns in matrix B and C.
			nr_cols_A,						// Number of columns and rows in matrix A and B respectively.
			&alf,							// Scalar used for multiplication.
			(const cuComplex **)d_arr_A,	// Weight array of pointers.
			lda,							// Leading dimension of each batch or matrix in array A.
			(const cuComplex **)d_arr_B,	// Data array of pointers.
			ldb,							// Leading dimension of each batch or matrix in array B.
			&bet,							// Scalar used for multiplication.
			(cuComplex **)d_arr_C,			// Output array of pointers.
			ldc,							// Leading dimension of each batch or matrix in array C.
			batchCount);					// Number of batches in each array.

324 325 326 327 328
	if (stat == CUBLAS_STATUS_INVALID_VALUE) {
		printf("RTBF: Invalid CUBLAS values\n");
	} else if (stat == CUBLAS_STATUS_EXECUTION_FAILED) {
		printf("RTBF: Execution failed.\n");
	}
Mark Ruzindana's avatar
Mark Ruzindana committed
329 330 331 332 333 334 335

	if(stat != CUBLAS_STATUS_SUCCESS){
		cerr << "cublasCgemmBatched failed" << endl;
		exit(1);
	}
	assert(!cudaGetLastError());

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}

__global__ 
void transpose(signed char* data, signed char* tra_data) {
	int i = threadIdx.x; 
	int c = threadIdx.y;

	int m = blockIdx.x;
	int f = blockIdx.y;
	int t = blockIdx.z;

	//int Nm = gridDim.x; // number of mcnts (packets)
	int Nf = gridDim.y; // number of f-engines (ROACHES)
	int Nt = gridDim.z; // time samples per mcnt

	int Ni = blockDim.x; // inputs per f-engine (aka antenna elements per ROACH)
	int Nc = blockDim.y; // bins per mcnt

	int in_idx  = i + Ni*c + Nc*Ni*t + Nt*Nc*Ni*f + Nf*Nt*Nc*Ni*m;	
	int out_idx = i + Ni*f + Nf*Ni*c + Nc*Nf*Ni*t + Nt*Nc*Nf*Ni*m;
Mark Ruzindana's avatar
Mark Ruzindana committed
356

357 358
	tra_data[2*out_idx] = data[2*in_idx];
	tra_data[2*out_idx + 1] = data[2*in_idx+1];
Mark Ruzindana's avatar
Mark Ruzindana committed
359

360
	return;
Mark Ruzindana's avatar
Mark Ruzindana committed
361 362
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
__global__
void data_restructure(signed char * data, cuComplex * data_restruc){

	/*
		Repurpose the transpose thread in the hashpipe codes by performing the transpose in the GPU.
		The motivation was, why transpose then transpose again? Why not just perform one transpose
		in the GPU which would be faster anyway.
	*/

	int i = threadIdx.x; 
	int c = threadIdx.y;

	int m = blockIdx.x;
	int f = blockIdx.y;
	int t = blockIdx.z;

	int Nm = gridDim.x; // number of mcnts (packets)
	int Nf = gridDim.y; // number of f-engines (ROACHES)
	int Nt = gridDim.z; // time samples per mcnt

	int Ni = blockDim.x; // inputs per f-engine (aka antenna elements per ROACH)
	int Nc = blockDim.y; // bins per mcnt

	int in_idx  = i + Ni*c + Nc*Ni*t + Nt*Nc*Ni*f + Nf*Nt*Nc*Ni*m;
	int out_idx = i + Ni*f + Nf*Ni*t + Nt*Nf*Ni*m + Nm*Nt*Nf*Ni*c;

	data_restruc[out_idx].x = data[2*in_idx]*1.0f;
	data_restruc[out_idx].y = data[2*in_idx + 1]*1.0f;

	return;

	/*
	// Original Code
	int e = threadIdx.x;
	int t = blockIdx.x;
	int f = blockIdx.y;

	//Restructure data so that the frequency bin is the slowest moving index
	data_restruc[f*BN_TIME*BN_ELE_BLOC + t*BN_ELE_BLOC + e].x = data[2*(t*BN_BIN*BN_ELE_BLOC + f*BN_ELE_BLOC + e)]*1.0f;
	data_restruc[f*BN_TIME*BN_ELE_BLOC + t*BN_ELE_BLOC + e].y = data[2*(t*BN_BIN*BN_ELE_BLOC + f*BN_ELE_BLOC + e) + 1]*1.0f;
	

	return;
	*/
}


Mark Ruzindana's avatar
Mark Ruzindana committed
410 411 412 413 414 415 416 417
__global__
void sti_reduction(cuComplex * data_in, float * data_out) {

	int f = blockIdx.x;
	int b = blockIdx.y;
	int t = threadIdx.x;
	int s = blockIdx.z;

418
	int h = sample_idx(s*BN_TIME_STI + t,b,f);						// Preprocessor macro used for the output of the beamformer. More detail can be seen in the header file. (First set of beams)
419
	int h1 = sample_idx(s*BN_TIME_STI + t,b+BN_BEAM1,f);			// Preprocessor macro used for the output of the beamformer. More detail can be seen in the header file. (Last set of beams)
Mark Ruzindana's avatar
Mark Ruzindana committed
420 421 422 423 424 425 426 427 428

	// Temporary variables used for updating.
	float beam_power1;
	float beam_power2;
	float cross_power1;
	float cross_power2;

	cuFloatComplex samp1;
	cuFloatComplex samp2;
429
	float scale = 1.0/BN_TIME_STI; 									// Scale power by number of samples per STI window.
Mark Ruzindana's avatar
Mark Ruzindana committed
430

431 432
	__shared__ cuFloatComplex reduced_array1[BN_STI_BLOC];
	__shared__ cuFloatComplex reduced_array[BN_STI_BLOC];
Mark Ruzindana's avatar
Mark Ruzindana committed
433

434
	if (t < BN_TIME_STI) {
Mark Ruzindana's avatar
Mark Ruzindana committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		// X polarization (XX*).
		samp1.x = data_in[h].x;
		samp1.y = data_in[h].y;
		beam_power1 = (samp1.x * samp1.x) + (samp1.y * samp1.y);	// Beamformer output multiplied by its conjugate (absolute value squared).
		reduced_array[t].x = beam_power1;

		// Y polarization (YY*).
		samp2.x = data_in[h1].x;
		samp2.y = data_in[h1].y;
		beam_power2 = (samp2.x * samp2.x) + (samp2.y * samp2.y);	// Beamformer output multiplied by its conjugate (absolute value squared).
		reduced_array[t].y = beam_power2;

		// Cross polarization (XY*).
		cross_power1 = (samp1.x * samp2.x) + (samp1.y * samp2.y);	// Real part of cross polarization.
		cross_power2 = (samp1.y * samp2.x) - (samp1.x * samp2.y);	// Imaginary part of cross polarization.
		reduced_array1[t].x = cross_power1;
		reduced_array1[t].y = cross_power2;
	}
	else{
		reduced_array[t].x = 0.0;
		reduced_array[t].y = 0.0;
		reduced_array1[t].x = 0.0;
		reduced_array1[t].y = 0.0;
	}
	__syncthreads();

	// Reduction is performed by splitting up the threads in each block and summing them all up.
	// The number of threads in each block needs to be a power of two in order for the reduction to work. (No left over threads).
	for(int k = blockDim.x/2; k>0; k>>=1){
		if(t<k){
			reduced_array[t].x += reduced_array[t+k].x;
			reduced_array[t].y += reduced_array[t+k].y;
			reduced_array1[t].x += reduced_array1[t+k].x;
			reduced_array1[t].y += reduced_array1[t+k].y;
		}
		__syncthreads();
	}

	// After reduction is complete, assign each reduced to value to appropriate position in output array.
	if(t == 0){
		data_out[output_idx(0,b,s,f)] = reduced_array[0].x*scale; 	// XX*.
		data_out[output_idx(1,b,s,f)] = reduced_array[0].y*scale; 	// YY*.
		data_out[output_idx(2,b,s,f)] = reduced_array1[0].x*scale; 	// XY* real.
		data_out[output_idx(3,b,s,f)] = reduced_array1[0].y*scale;	// XY* imaginary.
	}
480 481

	return;
Mark Ruzindana's avatar
Mark Ruzindana committed
482 483
}

484
void run_beamformer(signed char * data_in, float * data_out) {
485 486

	cudaError_t err_code;
Mark Ruzindana's avatar
Mark Ruzindana committed
487
	// Specify grid and block dimensions
488 489
	dim3 dimBlock(BN_STI_BLOC, 1, 1);
	dim3 dimGrid(BN_BIN, BN_BEAM1, BN_STI);
Mark Ruzindana's avatar
Mark Ruzindana committed
490

491
	// Specify grid and block dimensions
492
	dim3 dimBlock_d(BN_ELE_BLOC, 1, 1);
493
	dim3 dimGrid_d(BN_TIME, BN_BIN, 1);
494

495 496 497 498 499 500 501 502 503 504 505
	int Nm = 200;
	int Nf = 8;
	int Nt = 20;
	int Nc = 25;
	int Ni = 8;
	dim3 gridDim_transpose(Nm, Nf, Nt);
	dim3 blockDim_transpose(Ni, Nc, 1);

	signed char* d_tra_data_in = d_data1;
	//signed char* d_tra_data_out = d_data2;
	//signed char * d_restruct_in = d_data1;
506 507
	cuComplex * d_restruct_out = d_data;

508 509 510 511 512 513
	//cudaMemcpy(d_restruct_in, data_in, 2*BN_SAMP*sizeof(signed char), cudaMemcpyHostToDevice);
	cudaMemcpy(d_tra_data_in, data_in, 2*BN_SAMP*sizeof(signed char), cudaMemcpyHostToDevice);
	err_code = cudaGetLastError();
	if (err_code != cudaSuccess) {
		printf("RTBF: cudaMemcpy Failed: %s\n", cudaGetErrorString(err_code));
	}
514

515 516 517 518 519
	// Transpose the data
	// transpose<<<gridDim_transpose, blockDim_transpose>>>(d_tra_data_in, d_tra_data_out);
	// if (err_code != cudaSuccess) {
	// 	printf("RTBF: CUDA Error (transpose): %s\n", cudaGetErrorString(err_code));
	// }
520

521 522 523 524 525 526 527
	// Restructure data for cublasCgemmBatched function.
	data_restructure<<<dimGrid_d, dimBlock_d>>>(d_tra_data_in, d_restruct_out);
	//data_restructure<<<gridDim_transpose, blockDim_transpose>>>(d_restruct_in, d_restruct_out);
	//data_restructure<<<dimGrid_d, dimBlock_d>>>(d_restruct_in, d_restruct_out);
	if (err_code != cudaSuccess) {
		printf("RTBF: CUDA Error (data_restructure): %s\n", cudaGetErrorString(err_code));
	}
Mark Ruzindana's avatar
Mark Ruzindana committed
528 529

	// Call beamformer function containing cublasCgemmBatched()
530
	beamform();
531
	err_code = cudaGetLastError();
Mark Ruzindana's avatar
Mark Ruzindana committed
532 533 534 535
	if (err_code != cudaSuccess) {
		printf("CUDA Error (beamform): %s\n", cudaGetErrorString(err_code));
	}

536 537
	cuComplex * d_sti_in = d_beamformed;
	float * d_sti_out = d_outputs;
Mark Ruzindana's avatar
Mark Ruzindana committed
538 539

	// Call STI reduction kernel.
540
	sti_reduction<<<dimGrid, dimBlock>>>(d_sti_in, d_sti_out);
Mark Ruzindana's avatar
Mark Ruzindana committed
541 542 543 544 545 546 547

	err_code = cudaGetLastError();
	if (err_code != cudaSuccess) {
		printf("CUDA Error (sti_reduction): %s\n", cudaGetErrorString(err_code));
	}

	// Copy output data from device to host.
548
	cudaMemcpy(data_out, d_sti_out, BN_POL*(BN_OUTPUTS*sizeof(float)/2),cudaMemcpyDeviceToHost);
Mark Ruzindana's avatar
Mark Ruzindana committed
549

550
	return;
Mark Ruzindana's avatar
Mark Ruzindana committed
551
}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567


void rtbfCleanup() {
	// Free up GPU memory at the end of a program
	if (d_beamformed != NULL) {
		cudaFree(d_beamformed);
	}

	if (d_data != NULL) {
		cudaFree(d_data);
	}

	if (d_data1 != NULL) {
		cudaFree(d_data1);
	}

568 569 570 571
	if (d_data2 != NULL) {
		cudaFree(d_data2);
	}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	if (d_outputs != NULL) {
		cudaFree(d_outputs);
	}

	if (d_weights != NULL) {
		cudaFree(d_weights);
	}

	if (d_arr_A != NULL) {
		cudaFree(d_arr_A);
	}

	if (d_arr_B != NULL) {
		cudaFree(d_arr_B);
	}

	if (d_arr_C != NULL) {
		cudaFree(d_arr_C);
	}
	// Free up and release cublas handle
	cublasDestroy(handle);
593

594
}