Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matlab #1142

Merged
merged 16 commits into from
Feb 6, 2019
Merged

Matlab #1142

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions bindings/Matlab/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
#MEXLIBS="LDFLAGS=${ADIOS_LIBS}"

### TITAN
#ADIOS_DIR=/ccs/proj/e2e/pnorbert/ADIOS/ADIOS2/build.titan.gnu-nompi-shared/install
ADIOS_DIR=/ccs/proj/e2e/pnorbert/ADIOS/ADIOS2/build.titan.gnu-nompi/install
#ADIOS_DIR=/ccs/proj/e2e/pnorbert/ADIOS/ADIOS2/build.rhea.gnu-nompi/install
#ADIOS_LIBS=-Wl,-rpath=${ADIOS_DIR}/lib -L${ADIOS_DIR}/lib -ladios2
ADIOS_LIBS=-L${ADIOS_DIR}/lib -ladios2
#ADIOS_INC=-I${ADIOS_DIR}/include
#MEXLIBS=${ADIOS_LIBS}
MEXLIBS=${ADIOS_LIBS}

### Kathleen's PC
ADIOS_DIR=/opt/adios/2.2-lean
ADIOS_LIBS=-Wl,-rpath=${ADIOS_DIR}/lib -L${ADIOS_DIR}/lib -ladios2
#ADIOS_DIR=/opt/adios/2.2-lean
#ADIOS_LIBS=-Wl,-rpath=${ADIOS_DIR}/lib -L${ADIOS_DIR}/lib -ladios2
ADIOS_INC=-I${ADIOS_DIR}/include
MEXLIBS="LDFLAGS=${ADIOS_LIBS}"
#MEXLIBS="LDFLAGS=${ADIOS_LIBS}"


MEXOPTS=-largeArrayDims -DDEBUG CFLAGS="-g -std=c99 -fPIC"
MEXOPTS=-largeArrayDims -DDEBUG CFLAGS="-g -std=c99 -fPIC -O0"
default:
@echo "ADIOS reader methods for MATLAB"
@echo "Have 'mex' compiler in the path and run"
Expand Down
225 changes: 198 additions & 27 deletions bindings/Matlab/adiosopenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
static int verbose = 0;

mxClassID adiostypeToMatlabClass(int adiostype, mxComplexity *complexity);
size_t adiostypeToMemSize(adios2_type adiostype);
mxClassID adiostypestringToMatlabClass(const char *type,
mxComplexity *complexity);
mxArray *valueToMatlabValue(const void *data, mxClassID mxtype,
mxComplexity complexFlag);
mxArray *arrayToMatlabArray(const void *data, const size_t nelems, mxClassID mxtype,
mxComplexity complexFlag);
void errorCheck(int nlhs, int nrhs, const mxArray *prhs[]);
char *getString(const mxArray *mxstr);
static size_t *swap_order(size_t n, const size_t *array);
Expand Down Expand Up @@ -232,17 +235,18 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexPrintf(" Variables\n");
for (vi = 0; vi < nvars; vi++)
{
const adios2_variable *avar = adios_vars[vi];
adios2_variable *avar = adios_vars[vi];
/* field NAME */
size_t namelen;
const char varname[adios2_string_array_element_max_size];
char varname[adios2_string_array_element_max_size];

adios2_variable_name(varname, &namelen, avar);
varname[namelen] = '\0';
mxSetFieldByNumber(vars, vi, var_field_Name, mxCreateString(varname));
/* field TYPE */
adios2_type *adiostype;
adios2_variable_type(adiostype, avar);
mxtype = adiostypeToMatlabClass(*adiostype, &complexFlag);
adios2_type adiostype;
adios2_variable_type(&adiostype, avar);
mxtype = adiostypeToMatlabClass(adiostype, &complexFlag);
arr = mxCreateNumericMatrix(1, 1, mxtype, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_Type,
mxCreateString(mxGetClassName(arr)));
Expand Down Expand Up @@ -292,16 +296,31 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
arr = valueToMatlabValue((void *)(&stepsCount), mxINT64_CLASS, mxREAL);
mxSetFieldByNumber(vars, vi, var_field_StepsCount, arr);

/* field GLOBALMIN */
// FIXME arr = valueToMatlabValue(vinfo->gmin, mxtype, complexFlag);
double fakemin = 0.0;
arr = valueToMatlabValue(&fakemin, mxDOUBLE_CLASS, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMin, arr);

/* field GLOBALMAX */
// FIXME arr = valueToMatlabValue(vinfo->gmax, mxtype, complexFlag);
arr = valueToMatlabValue(&fakemin, mxDOUBLE_CLASS, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMax, arr);
/* fields GLOBALMIN and GLOBALMAX */
if (adiostype == adios2_type_string)
{
char *str1 = (char *)mxCalloc(65536, sizeof(char));
adios2_get(fp, avar, str1, adios2_mode_sync);
arr = valueToMatlabValue(str1, mxtype, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMin, arr);
/* must make another copy to avoid double free corruption
* at exit of Matlab */
arr = valueToMatlabValue(str1, mxtype, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMax, arr);
mxFree(str1);
}
else
{
size_t typesize = adiostypeToMemSize(adiostype);
char value[typesize];
adios2_variable_min(value, avar);
arr = valueToMatlabValue(value, mxtype, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMin, arr);

adios2_variable_max(value, avar);
arr = valueToMatlabValue(value, mxtype, complexFlag);
mxSetFieldByNumber(vars, vi, var_field_GlobalMax, arr);
}

free(dims);
}
Expand All @@ -316,31 +335,84 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
const adios2_attribute *aa = adios_attrs[ai];
/* field NAME */
size_t namelen;
const char attrname[adios2_string_array_element_max_size];
char attrname[adios2_string_array_element_max_size];
adios2_attribute_name(attrname, &namelen, aa);
attrname[namelen] = '\0';

mxSetFieldByNumber(attrs, ai, attr_field_Name,
mxCreateString(attrname));
/* field TYPE */
size_t typelen;
const char atype[adios2_string_array_element_max_size];
adios2_type adiostype;
adios2_attribute_type(&adiostype, aa);

/*char atype[adios2_string_array_element_max_size];
adios2_attribute_type_string(atype, &typelen, aa);
atype[namelen] = '\0';*/

mxtype = adiostypestringToMatlabClass(atype, &complexFlag);
// mxtype = adiostypeToMatlabClass(adiostype, &complexFlag);
mxtype = adiostypeToMatlabClass(adiostype, &complexFlag);
arr = mxCreateNumericMatrix(1, 1, mxtype, complexFlag);
mxSetFieldByNumber(attrs, ai, attr_field_Type,
mxCreateString(mxGetClassName(arr)));
mxDestroyArray(arr);
/* field VALUE */
size_t asize;
const void *data;
adios2_attribute_data(data, &asize, aa);
arr = valueToMatlabValue(data, mxtype, complexFlag);
mxSetFieldByNumber(attrs, ai, attr_field_Value, arr);
size_t aelems;
mexPrintf(" get field Data\n");
adios2_attribute_size(&aelems, aa);
size_t atypesize = adiostypeToMemSize(adiostype);
mexPrintf(" field Data nelems = %zu, type size=%zu\n", aelems, atypesize);
void *data;
if (adiostype == adios2_type_string)
{
if (aelems == 1)
{
/* single string attribute */
/* FIXME: get the size of string from ADIOS first */
data = mxCalloc(4096, 1);
size_t nelems;
adios2_attribute_data(data, &nelems, aa);
mexPrintf(" field Data ptr=%p data=%s\n", data, data);
arr = valueToMatlabValue(data, mxtype, complexFlag);
}
else
{
/* string array attribute */
char **dataArray = mxCalloc(aelems, sizeof(char*));
size_t i;
for (i = 0; i < aelems; ++i)
{
dataArray[i] = mxCalloc(4096, 1);
}
size_t nelems;
data = (void *) dataArray;
adios2_attribute_data(data, &nelems, aa);
mexPrintf(" field Data ptr=%p data[0]=%s\n", data, *(char**)data);
arr = arrayToMatlabArray(data, aelems, mxtype, complexFlag);
}
mxSetFieldByNumber(attrs, ai, attr_field_Value, arr);
}
else
{
data = mxCalloc(atypesize, aelems);
size_t nelems;
adios2_attribute_data(data, &nelems, aa);
mexPrintf(" field Data ptr=%p nelems=%zu\n", data, nelems);
if (aelems == 1)
{
/* single element attribute */
arr = valueToMatlabValue(data, mxtype, complexFlag);
}
else
{
/* array attribute */
arr = arrayToMatlabArray(data, aelems, mxtype, complexFlag);
}
mxSetFieldByNumber(attrs, ai, attr_field_Value, arr);
}
if (verbose > 1)
mexPrintf(" %s: adios type=%s size=%zu\n", attrname, atype,
asize);
mexPrintf(" %s: adios type=%s size=%zu\n", attrname, mxGetClassName(arr),
aelems);
mxFree(data);
}

if (verbose)
Expand Down Expand Up @@ -385,6 +457,45 @@ mxArray *valueToMatlabValue(const void *data, mxClassID mxtype,
return arr;
}

mxArray *arrayToMatlabArray(const void *data, const size_t nelems, mxClassID mxtype,
mxComplexity complexFlag)
{
/* copies values in all cases, so one can free(data) later */
mxArray *arr;
if (data == NULL)
{
arr = mxCreateString("undefined");
}
else if (mxtype == mxCHAR_CLASS)
{
arr = mxCreateCharMatrixFromStrings(nelems, (const char**)data);
}
else if (complexFlag == mxCOMPLEX)
{
arr = mxCreateDoubleMatrix(1, nelems, mxCOMPLEX);
size_t i;
for (i = 0; i < nelems; ++i)
{
if (mxtype == mxSINGLE_CLASS)
{
((float *)mxGetPr(arr))[i] = ((const float *)data)[2*i];
((float *)mxGetPi(arr))[i] = ((const float *)data)[2*i+1];
}
else
{
((double *)mxGetPr(arr))[i] = ((const double *)data)[2*i];
((double *)mxGetPi(arr))[i] = ((const double *)data)[2*i+1];
}
}
}
else
{
arr = mxCreateNumericMatrix(1, nelems, mxtype, mxREAL);
memcpy(mxGetData(arr), data, nelems*mxGetElementSize(arr));
}
return arr;
}

void errorCheck(int nlhs, int nrhs, const mxArray *prhs[])
{
/* Assume that we are called from adiosread.m which checks the arguments
Expand Down Expand Up @@ -447,7 +558,7 @@ mxClassID adiostypeToMatlabClass(adios2_type adiostype,
return mxINT8_CLASS;

case adios2_type_string:
case adios2_type_string_array:
/* case adios2_type_string_array: */
return mxCHAR_CLASS;

case adios2_type_unsigned_short:
Expand Down Expand Up @@ -503,6 +614,66 @@ mxClassID adiostypeToMatlabClass(adios2_type adiostype,
return 0; /* just to avoid warnings. never executed */
}

/** return the appropriate class for an adios type (and complexity too) */
size_t adiostypeToMemSize(adios2_type adiostype)
{
switch (adiostype)
{
case adios2_type_unsigned_char:
case adios2_type_uint8_t:
case adios2_type_char:
case adios2_type_signed_char:
case adios2_type_int8_t:
return sizeof(char);

case adios2_type_string:
/* case adios2_type_string_array: */
return sizeof(char);

case adios2_type_unsigned_short:
case adios2_type_uint16_t:
case adios2_type_short:
case adios2_type_int16_t:
return sizeof(int16_t);

case adios2_type_unsigned_int:
case adios2_type_uint32_t:
case adios2_type_int:
case adios2_type_int32_t:
return sizeof(int32_t);

case adios2_type_unsigned_long_int:
case adios2_type_long_int:
if (sizeof(long int) == 4)
return sizeof(int32_t);
else
return sizeof(int64_t);

case adios2_type_unsigned_long_long_int:
case adios2_type_uint64_t:
case adios2_type_long_long_int:
case adios2_type_int64_t:
return sizeof(int64_t);

case adios2_type_float:
return sizeof(float);
case adios2_type_double:
return sizeof(double);

case adios2_type_float_complex: /* 8 bytes */
return 2*sizeof(float);
case adios2_type_double_complex: /* 16 bytes */
return 2*sizeof(double);

default:
mexErrMsgIdAndTxt("MATLAB:adiosopenc.c:dimensionTooLarge",
"Adios type id=%d not supported in matlab.\n",
adiostype);
break;
}
return 0; /* just to avoid warnings. never executed */
}

/** return the appropriate class for an adios type (and complexity too) */
mxClassID adiostypestringToMatlabClass(const char *type,
mxComplexity *complexity)
Expand Down
12 changes: 9 additions & 3 deletions bindings/Matlab/adiosreadc.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ mxArray *readdata(adios2_engine *fp, adios2_io *group, const char *path,
if (adiostype == adios2_type_string)
{
/* Matlab string != char array of C, so handle separately */
data = (void *)mxCalloc(varDim[0], sizeof(char));
data = (void *)mxCalloc(65536, sizeof(char));
mexPrintf("Create C string reading in a string with ptr %p\n", data);
}
else
{
Expand Down Expand Up @@ -237,7 +238,11 @@ mxArray *readdata(adios2_engine *fp, adios2_io *group, const char *path,
mexPrintf("Set step-selection for variable: start = %zu count = %zu\n",
qstepstart, qstepcount);
}
adios2_set_selection(avar, varNdim, qoffsets, qcounts);

if (varNdim > 0)
{
adios2_set_selection(avar, varNdim, qoffsets, qcounts);
}
adios2_set_step_selection(avar, qstepstart, qstepcount);

/* read in data */
Expand All @@ -248,6 +253,7 @@ mxArray *readdata(adios2_engine *fp, adios2_io *group, const char *path,

if (adiostype == adios2_type_string)
{
mexPrintf("Create Matlab string from C string [%s]\n", (char*)data);
out = mxCreateString((char *)data);
mxFree(data);
}
Expand Down Expand Up @@ -343,7 +349,7 @@ mxClassID adiostypeToMatlabClass(adios2_type adiostype,
return mxINT8_CLASS;

case adios2_type_string:
case adios2_type_string_array:
/* case adios2_type_string_array: */
return mxCHAR_CLASS;

case adios2_type_unsigned_short:
Expand Down
Loading