Skip to content

Commit

Permalink
Fixes trigger event and interface so it works properly with domains
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenNneji committed Sep 21, 2023
1 parent d171596 commit 1ed16e9
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 328 deletions.
45 changes: 29 additions & 16 deletions API/events/triggerEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,35 @@ function triggerEvent(eventType, data)
coder.ceval('std::mem_fn(&eventHelper::sendMessage)', helper, [data,0]);
elseif strcmpi(eventType, 'plot')
result = data{1};
problemDef = data{3};
nContrast = length(result{1});
[reflect, nReflect] = packCellArray(result{1}); % reflectivity
[shiftedData, nShiftedData] = packCellArray(result{3});
[sldProfiles, nSldProfiles] = packCellArray(result{5});
[layers, nLayers] = packCellArray(result{6}); % All Layers

[reflect, nReflect] = packCellArray(result{1}, 1); % reflectivity
[shiftedData, nShiftedData] = packCellArray(result{3}, 1);

switch problemDef.TF
case 'domains'
[sldProfiles, nSldProfiles] = packCellArray(result{5}, 1);
[sldProfiles2, nSldProfiles2] = packCellArray(result{5}, 2);
[layers, nLayers] = packCellArray(result{6}, 1);
[layers2, nLayers2] = packCellArray(result{6}, 2);
otherwise
[sldProfiles, nSldProfiles] = packCellArray(result{5}, 1);
[layers, nLayers] = packCellArray(result{6}, 1);
sldProfiles2 = coder.nullcopy(zeros(0));
nSldProfiles2 = coder.nullcopy(zeros(0));
layers2 = coder.nullcopy(zeros(0));
nLayers2 = coder.nullcopy(zeros(0));
end

ssubs = data{2}; % ssubs

problemDef = data{3};
modelType = [problemDef.modelType, 0];
resample = problemDef.resample;
dataPresent = problemDef.dataPresent;

coder.ceval('std::mem_fn(&eventHelper::updatePlot)', helper, nContrast, reflect, nReflect, shiftedData, ...
nShiftedData, sldProfiles, nSldProfiles, layers, nLayers, ssubs, resample, dataPresent, ...
modelType);
nShiftedData, sldProfiles, nSldProfiles, layers, nLayers, sldProfiles2, nSldProfiles2, layers2, ...
nLayers2, ssubs, resample, dataPresent, modelType);
end
notified = false;
else
Expand All @@ -69,26 +82,26 @@ function triggerEvent(eventType, data)
end


function [packedArray, counts] = packCellArray(cellArray)
% Packs a cell array with different sized arrays into a single row
% array and an array of counts for each cell. For the example below
function [packedArray, counts] = packCellArray(cellArray, col)
% Packs a specified column of a cell array with different sized arrays into a
% single row array and an array of counts for each cell. For the example below
% reflect will be [1, 2, 3, 4, 5, 6, 7] and nReflect will be [3, 4]
%
% [reflect, nReflect] = packCellArray({[1, 2, 3], [4, 5, 6, 7]});
% [reflect, nReflect] = packCellArray({[1; 2; 3], [4; 5; 6; 7]}, 1);
rowSize = 0;
nCells = length(cellArray);
nCells = size(cellArray, 1);

counts = zeros(nCells, 1);
for i=1:nCells
counts(i) = size(cellArray{i}, 1) * size(cellArray{i}, 2);
counts(i) = size(cellArray{i, col}, 1) * size(cellArray{i, col}, 2);
rowSize = rowSize + counts(i);
end

packedArray = zeros(rowSize, 1);
start = 1;
for i=1:nCells
stop = start + counts(i);
packedArray(start:stop-1, :) = reshape(cellArray{i}, [], 1);
packedArray(start:stop-1, :) = reshape(cellArray{i, col}, [], 1);
start = stop;
end
end
end
82 changes: 0 additions & 82 deletions Jenkinsfile

This file was deleted.

7 changes: 5 additions & 2 deletions compile/events/eventHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ class eventHelper
};

void updatePlot(int nContrast, double* reflect, double* nReflect, double* shiftedData, double* nShiftedData,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers, double* ssubs,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers,
double* sldProfiles2, double* nSldProfiles2, double* layers2, double* nLayers2, double* ssubs,
double* resample, double* dataPresent, const char* modelType)
{
auto updatePlot = library->get_function<void(int, double*, double*, double*, double*,
double*, double*, double*, double*, double*,
double*, double*, double*, double*,
double*, double*, const char*)>("updatePlot");

// pass the arguments to the function
return updatePlot(nContrast, reflect, nReflect, shiftedData, nShiftedData, sldProfiles, nSldProfiles,
layers, nLayers, ssubs, resample, dataPresent, modelType);
layers, nLayers, sldProfiles2, nSldProfiles2, layers2, nLayers2, ssubs, resample,
dataPresent, modelType);

};
};
Expand Down
7 changes: 6 additions & 1 deletion compile/events/eventManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ LIB_EXPORT void sendMessage(const char* msg)
}

LIB_EXPORT void updatePlot(int nContrast, double* reflect, double* nReflect, double* shiftedData, double* nShiftedData,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers, double* ssubs,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers,
double* sldProfiles2, double* nSldProfiles2, double* layers2, double* nLayers2, double* ssubs,
double* resample, double* dataPresent, const char* modelType)
{
plotData data;
Expand All @@ -19,6 +20,10 @@ LIB_EXPORT void updatePlot(int nContrast, double* reflect, double* nReflect, dou
data.nSldProfiles = nSldProfiles;
data.layers = layers;
data.nLayers = nLayers;
data.sldProfiles2 = sldProfiles2;
data.nSldProfiles2 = nSldProfiles2;
data.layers2 = layers2;
data.nLayers2 = nLayers2;
data.ssubs = ssubs;
data.resample = resample;
data.dataPresent = dataPresent;
Expand Down
3 changes: 2 additions & 1 deletion compile/events/eventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ extern "C" {
#endif

LIB_EXPORT void updatePlot(int nContrast, double* reflect, double* nReflect, double* shiftedData, double* nShiftedData,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers, double* ssubs,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers,
double* sldProfiles2, double* nSldProfiles2, double* layers2, double* nLayers2, double* ssubs,
double* resample, double* dataPresent, const char* modelType);

LIB_EXPORT void sendMessage(const char* msg);
Expand Down
4 changes: 4 additions & 0 deletions compile/events/eventManagerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct plotData {
double* nSldProfiles;
double* layers;
double* nLayers;
double* sldProfiles2;
double* nSldProfiles2;
double* layers2;
double* nLayers2;
double* ssubs;
double* resample;
double* dataPresent;
Expand Down
96 changes: 46 additions & 50 deletions compile/events/eventManagerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ void initDyLib(void)
}
}

mxArray* reshapeAndAddDataToCell(int cellRow, int cellCol, double* data, double* nData,
double* data2, double* nData2, int dataCol)
{
mwSize dims[2] = {0, 0};
int offset = 0;
size_t bytes_to_copy;
mxArray* cellArray = mxCreateCellMatrix(cellRow, cellCol);
for ( int i = 0; i < cellRow; i++){
dims[0] = (mwSize)nData[i] / dataCol;
dims[1] = dataCol;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), data + offset, bytes_to_copy);
mxSetCell(cellArray, i, temp);
offset += dims[0] * dims[1];
}

if (data2 != NULL && nData != NULL)
{
offset = 0;
for ( int i = 0; i < cellRow; i++){
dims[0] = (mwSize)nData2[i] / dataCol;
dims[1] = dataCol;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), data2 + offset, bytes_to_copy);
mxSetCell(cellArray, i + cellRow, temp);
offset += dims[0] * dims[1];
}
}

return cellArray;
}

// Callback function
void myCallback(const baseEvent& event)
{
Expand All @@ -28,60 +62,21 @@ void myCallback(const baseEvent& event)
} else if (event.type == PLOT){
plotEvent* pEvent = (plotEvent*)&event;

mxArray *reflect = mxCreateCellMatrix(pEvent->data->nContrast, 1);
mxArray *reflect = reshapeAndAddDataToCell(pEvent->data->nContrast, 1,
pEvent->data->reflect, pEvent->data->nReflect, NULL, NULL, 2);

mwSize dims[2] = {0, 0};
int offset = 0;;
size_t bytes_to_copy;

for ( int i = 0; i < pEvent->data->nContrast; i++){
dims[0] = (mwSize)pEvent->data->nReflect[i]/2.0;
dims[1] = 2;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), pEvent->data->reflect + offset, bytes_to_copy);
mxSetCell(reflect, i, temp);
offset += dims[0] * dims[1];
}
mxArray *shifted = reshapeAndAddDataToCell(pEvent->data->nContrast, 1,
pEvent->data->shiftedData, pEvent->data->nShiftedData, NULL, NULL, 3);

mxArray *shifted = mxCreateCellMatrix(pEvent->data->nContrast, 1);
offset = 0;
for ( int i = 0; i < pEvent->data->nContrast; i++){
dims[0] = (mwSize)pEvent->data->nShiftedData[i]/3.0;
dims[1] = 3;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), pEvent->data->shiftedData + offset, bytes_to_copy);
mxSetCell(shifted, i, temp);
offset += dims[0] * dims[1];
}

mxArray *slds = mxCreateCellMatrix(pEvent->data->nContrast, 1);
offset = 0;
for ( int i = 0; i < pEvent->data->nContrast; i++){
dims[0] = (mwSize)pEvent->data->nSldProfiles[i]/2.0;
dims[1] = 2;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), pEvent->data->sldProfiles + offset, bytes_to_copy);
mxSetCell(slds, i, temp);
offset += dims[0] * dims[1];
}
mxArray *slds = reshapeAndAddDataToCell(pEvent->data->nContrast, (pEvent->data->nSldProfiles2 == NULL) ? 1 : 2,
pEvent->data->sldProfiles, pEvent->data->nSldProfiles,
pEvent->data->sldProfiles2, pEvent->data->nSldProfiles2, 2);

mxArray *layers = mxCreateCellMatrix(pEvent->data->nContrast, 1);
offset = 0;
for ( int i = 0; i < pEvent->data->nContrast; i++){
dims[0] = (mwSize)pEvent->data->nLayers[i];
dims[1] = 1;
mxArray* temp = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
bytes_to_copy = dims[0] * dims[1] * mxGetElementSize(temp);
memcpy(mxGetPr(temp), pEvent->data->layers + offset, bytes_to_copy);
mxSetCell(layers, i, temp);
offset += dims[0] * dims[1];
}
mxArray *layers = reshapeAndAddDataToCell(pEvent->data->nContrast, (pEvent->data->nLayers2 == NULL) ? 1 : 2,
pEvent->data->layers, pEvent->data->nLayers,
pEvent->data->layers2, pEvent->data->nLayers, 2);

dims[0] = (mwSize)pEvent->data->nContrast;
dims[1] = 1;
mwSize dims[2] = {(mwSize)pEvent->data->nContrast, 1};
mxArray* ssubs = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
memcpy(mxGetPr(ssubs), pEvent->data->ssubs, dims[0] * mxGetElementSize(ssubs));

Expand Down Expand Up @@ -115,6 +110,7 @@ void myCallback(const baseEvent& event)
mxDestroyArray(prhs[1]);
}


// MEX Gateway
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
Expand Down
Loading

0 comments on commit 1ed16e9

Please sign in to comment.