-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nlesc-recruit/mex-integration
Mex integration
- Loading branch information
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <cstring> | ||
#include <cstdint> | ||
#include "mex.h" | ||
#include "tcbf.h" // include the header | ||
|
||
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { | ||
if (nrhs != 7) { | ||
mexErrMsgIdAndTxt("beamform:InvalidInput", | ||
"Seven inputs required: a_matrix, rf, bf, pixels, frames, samples, device_id"); | ||
} | ||
|
||
std::string path_a_matrix = mxArrayToString(prhs[0]); | ||
std::string path_rf = mxArrayToString(prhs[1]); | ||
std::string path_bf = mxArrayToString(prhs[2]); | ||
size_t pixels = mxGetScalar(prhs[3]); | ||
size_t frames = mxGetScalar(prhs[4]); | ||
size_t samples = mxGetScalar(prhs[5]); | ||
unsigned device_id = mxGetScalar(prhs[6]); | ||
|
||
cu::init(); | ||
cu::Device device(device_id); | ||
cu::Context context(CU_CTX_BLOCKING_SYNC, device); | ||
cu::Stream stream; | ||
|
||
tcbf::Beamformer beamformer(pixels, frames, samples, device, stream); | ||
cu::HostMemory RF(2 * frames * samples); | ||
cu::HostMemory BF(2 * pixels * frames * sizeof(int32_t)); | ||
|
||
beamformer.read_A_matrix(path_a_matrix); | ||
beamformer.read_RF(RF, path_rf); | ||
beamformer.process(RF, BF); | ||
beamformer.write_BF(BF, path_bf); | ||
|
||
mwSize dims[3] = {pixels, frames, 2}; | ||
mxArray *outArray = mxCreateNumericArray(3, dims, mxINT32_CLASS, mxREAL); | ||
int32_t *outData = reinterpret_cast<int32_t *>(mxGetData(outArray)); | ||
|
||
// Copy the data | ||
std::memcpy(outData, BF, 2 * frames * pixels * sizeof(int32_t)); | ||
|
||
// Assign output | ||
plhs[0] = outArray; | ||
|
||
int status = 0; | ||
|
||
if (nlhs > 1) { | ||
plhs[1] = mxCreateDoubleScalar((double)status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
% mex_compile_prepare_a_matrix.m | ||
% Adjust include and library paths as needed | ||
mexcuda -v ... | ||
-L"/usr/local/cuda/lib64" -lcudart -lcuda ... | ||
'NVCCFLAGS=-gencode=arch=compute_89,code=sm_89' ... | ||
-ltcbf -lccglib ... | ||
beamform_mex.cu | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
% mex_compile_prepare_a_matrix.m | ||
% Adjust include and library paths as needed | ||
mexcuda -v ... | ||
-L"/usr/local/cuda/lib64" -lcudart -lcuda ... | ||
'NVCCFLAGS=-gencode=arch=compute_89,code=sm_89' ... | ||
-lccglib ... | ||
prepare_a_matrix_mex.cu ../src/prepare_a_matrix.cu | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "../src/prepare_a_matrix.h" // include the header | ||
#include "mex.h" | ||
|
||
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { | ||
if (nrhs != 5) { | ||
mexErrMsgIdAndTxt("prepare_a_matrix_mex:InvalidInput", | ||
"Five inputs required: a_matrix_in, a_matrix_out, pixels, samples, device_id"); | ||
} | ||
|
||
char *a_matrix_in_c = mxArrayToString(prhs[0]); | ||
char *a_matrix_out_c = mxArrayToString(prhs[1]); | ||
double pixels_d = mxGetScalar(prhs[2]); | ||
double samples_d = mxGetScalar(prhs[3]); | ||
double device_id_d = mxGetScalar(prhs[4]); | ||
|
||
std::string a_matrix_in(a_matrix_in_c); | ||
std::string a_matrix_out(a_matrix_out_c); | ||
size_t pixels = static_cast<size_t>(pixels_d); | ||
size_t samples = static_cast<size_t>(samples_d); | ||
unsigned device_id = static_cast<unsigned>(device_id_d); | ||
|
||
mxFree(a_matrix_in_c); | ||
mxFree(a_matrix_out_c); | ||
|
||
int status = prepareAMatrix(a_matrix_in, a_matrix_out, pixels, samples, device_id); | ||
|
||
if (status == 0) { | ||
mexPrintf("prepareAMatrix completed successfully.\n"); | ||
} else { | ||
mexPrintf("prepareAMatrix failed with status code: %d\n", status); | ||
} | ||
|
||
if (nlhs > 0) { | ||
plhs[0] = mxCreateDoubleScalar((double)status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef PREPARE_A_MATRIX_H | ||
#define PREPARE_A_MATRIX_H | ||
|
||
#include <string> | ||
#include <cstddef> // for size_t | ||
|
||
int prepareAMatrix(const std::string &path_a_matrix_in, | ||
const std::string &path_a_matrix_out, | ||
size_t pixels, | ||
size_t samples, | ||
unsigned device_id); | ||
|
||
#endif // PREPARE_A_MATRIX_H |