Load data to CUDA


I have structure in Matlab, but need that data in CUDA. How should I load that data in .cu file?
In .cu file I want to have structure again.

Any help is appreciated.


You can make a mex file and call the mex file from matlab, passing in the structure as a parameter. Your mex file can be .cpp or .cu, as long as it has the mex entry point function (this replaces main):

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

The structure is passed as an mxArray type, and you can access individual fields of the structure from the mex file in a couple of ways, i.e.

int field_num = mxGetFieldNumber(obj, "field_name");
const double *val = static_cast<const double*>(mxGetData(mxGetFieldByNumber(obj, index, field_num)));

where obj is the struct passed into the mex file.

If you’re unfamiliar with mex files, this might be a good place to start:

EDIT: had an error in the mxGetData line.

Thank You for Your answer, but I think we have a misunderstanding here, unless I am wrong.
You talk about using .mex files, but I need to load data in C from Matlab, not vice versa. I have .mat file which I need to load in C…


The Matlab install provides you with some libs, headers and DLLs for accessing .MAT files straight from C. All you have to do is add the header includes, link with the libs and drop the DLLs in with your application. The examples they provide are at:


I haven’t personally done it that way round, but I have done storing multiple arrays from C++ into a .MAT file and I assume the reading the data from .MAT is just as straight forward.

using a mex file you can transfer data you have in matlab to a .c or .cpp file (as well as return data from that .c or .cpp file to matlab). I wasn’t aware of the possibility of reading a .mat file from outside of matlab, and if performance isn’t critical then it might be the best option. The only downside is that you’ll have to perform disk accesses

There is MAT-File Interface Library, yes, and it does not work. What am I doing wrong?
I have .mat file that has two arrays, i.e. when in Matlab I use

load file.mat

I get arrays A[Nx1] and B[Nx2].

This is what I get:
cuda_mat_load.cu(34): warning: variable “A” was set but never used
cuda_mat_load.cu(34): warning: variable “B” was set but never used
/tmp/tmpxft_00002d85_00000000-13_cuda_mat_load.o: In function main': tmpxft_00002d85_00000000-1_cuda_mat_load.cudafe1.cpp:(.text+0x1b): undefined reference to matOpen’
tmpxft_00002d85_00000000-1_cuda_mat_load.cudafe1.cpp:(.text+0x5d): undefined reference to matGetVariable' tmpxft_00002d85_00000000-1_cuda_mat_load.cudafe1.cpp:(.text+0x72): undefined reference to matGetVariable’
tmpxft_00002d85_00000000-1_cuda_mat_load.cudafe1.cpp:(.text+0x82): undefined reference to `matClose’

Thank you.

Oh, of course, my C code:


int main() {

/* loading data from .mat file */
        MATFile *struct_ptr = 0;
        struct_ptr = matOpen("file.mat", "r");
        if (struct_ptr == NULL ){
                printf("Error creating file");
        mxArray *stage = 0, *value = 0;
        A = matGetVariable(struct_ptr, "A");
        B = matGetVariable(struct_ptr, "B");
        return 0;