2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 #include "api_scilab.h"
16 #include "sci_malloc.h"
17 #include "serialization.h"
18 #include "elem_common.h"
20 static int serialize_double(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
33 if (isVarComplex(_pvCtx, _piAddr))
37 sciErr = getComplexMatrixOfDouble(_pvCtx, _piAddr, &iRows, &iCols, &pdblR, &pdblI);
40 printError(&sciErr, 0);
44 iOutLen = 4 + (2 * iRows * iCols * sizeof(double) / sizeof(int));
45 piOut = (int *)MALLOC(iOutLen * sizeof(int));
51 piOut[0] = sci_matrix;
54 piOut[3] = 1; //complex
56 //move 'p' to first real value
57 p = (double *)(piOut + 4);
58 iSize = iRows * iCols;
59 C2F(dcopy) (&iSize, pdblR, &iOne, p, &iOne);
60 //move 'p' to first complex value
61 p = p + iRows * iCols;
62 C2F(dcopy) (&iSize, pdblI, &iOne, p, &iOne);
68 sciErr = getMatrixOfDouble(_pvCtx, _piAddr, &iRows, &iCols, &pdblR);
71 printError(&sciErr, 0);
75 iOutLen = 4 + (iRows * iCols * sizeof(double) / sizeof(int));
76 piOut = (int *)MALLOC(iOutLen * sizeof(int));
82 piOut[0] = sci_matrix;
85 piOut[3] = 0; //not complex
87 //move 'p' to first value
88 p = (double *)(piOut + 4);
89 iSize = iRows * iCols;
90 C2F(dcopy) (&iSize, pdblR, &iOne, p, &iOne);
94 *_piBufferSize = iOutLen;
98 static int serialize_string(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
104 char **pstData = NULL;
108 int *piOutLen = NULL;
111 iErr = getAllocatedMatrixOfString(_pvCtx, _piAddr, &iRows, &iCols, &pstData);
117 for (i = 0; i < iRows * iCols; i++)
119 iOutLen += (int)strlen(pstData[i]);
124 iOutLen = iOutLen / (sizeof(int) / sizeof(char)) + 1;
128 iOutLen = iOutLen / (sizeof(int) / sizeof(char));
131 //4 for header and size of each string
132 iOutLen += 4 + iRows * iCols;
134 piOut = (int *)MALLOC(iOutLen * sizeof(int));
140 piOut[0] = sci_strings;
143 piOut[3] = 0; //not complex
145 piOutLen = piOut + 4;
147 for (i = 0; i < iRows * iCols; i++)
149 piOutLen[i] = (int)strlen(pstData[i]);
152 p = (char *)(piOut + 4 + iRows * iCols);
153 for (i = 0; i < iRows * iCols; i++)
155 memcpy(p, pstData[i], piOutLen[i]);
160 *_piBufferSize = iOutLen;
162 freeAllocatedMatrixOfString(iRows, iCols, pstData);
166 static int serialize_boolean(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
177 sciErr = getMatrixOfBoolean(_pvCtx, _piAddr, &iRows, &iCols, &piBool);
180 printError(&sciErr, 0);
184 //4 for header and 1 for each boolean
185 iOutLen = 4 + iRows * iCols;
187 piOut = (int *)MALLOC(iOutLen * sizeof(int *));
193 piOut[0] = sci_boolean;
196 piOut[3] = 0; //not complex
198 p = (int *)(piOut + 4);
199 memcpy(p, piBool, iRows * iCols * sizeof(int));
202 *_piBufferSize = iOutLen;
206 static int serialize_int(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
219 sciErr = getVarDimension(_pvCtx, _piAddr, &iRows, &iCols);
222 printError(&sciErr, 0);
226 sciErr = getMatrixOfIntegerPrecision(_pvCtx, _piAddr, &iPrecision);
229 printError(&sciErr, 0);
233 if (iPrecision == SCI_INT8 || iPrecision == SCI_UINT8)
235 iItemSize = sizeof(char);
237 else if (iPrecision == SCI_INT16 || iPrecision == SCI_UINT16)
239 iItemSize = sizeof(short);
241 else if (iPrecision == SCI_INT32 || iPrecision == SCI_UINT32)
243 iItemSize = sizeof(int);
246 else if(iPrecision == SCI_INT64 || iPrecision == SCI_UINT64)
248 iItemSize = sizeof(long long);
251 //check and adjust alignement on integer
252 iOutLen = iRows * iCols;
253 if ((iOutLen * iItemSize) % sizeof(int))
255 iOutLen = (iOutLen * iItemSize) / sizeof(int) + 1;
259 iOutLen = (iOutLen * iItemSize) / (sizeof(int));
263 piOut = (int *)MALLOC(iOutLen * sizeof(int *));
272 piOut[3] = iPrecision; //precision
278 sciErr = getMatrixOfInteger8(_pvCtx, _piAddr, &iRows, &iCols, (char **)&pvData);
283 sciErr = getMatrixOfUnsignedInteger8(_pvCtx, _piAddr, &iRows, &iCols, (unsigned char **)&pvData);
288 sciErr = getMatrixOfInteger16(_pvCtx, _piAddr, &iRows, &iCols, (short **)&pvData);
293 sciErr = getMatrixOfUnsignedInteger16(_pvCtx, _piAddr, &iRows, &iCols, (unsigned short **)&pvData);
298 sciErr = getMatrixOfInteger32(_pvCtx, _piAddr, &iRows, &iCols, (int **)&pvData);
303 sciErr = getMatrixOfUnsignedInteger32(_pvCtx, _piAddr, &iRows, &iCols, (unsigned int **)&pvData);
309 sciErr = getMatrixOfInteger64(_pvCtx, _piAddr, &iRows, &iCols, (long long**)&pvData);
314 sciErr = getMatrixOfUnsignedInteger64(_pvCtx, _piAddr, &iRows, &iCols, (unsigned long long**)&pvData);
325 printError(&sciErr, 0);
330 memcpy(p, pvData, iRows * iCols * iItemSize);
332 *_piBufferSize = iOutLen;
336 static int serialize_sparse(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize, BOOL _bData)
346 double *pdblR = NULL;
347 double *pdblI = NULL;
356 iComplex = isVarComplex(_pvCtx, _piAddr);
359 iRet = getAllocatedComplexSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos, &pdblR, &pdblI);
363 iRet = getAllocatedSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos, &pdblR);
369 iRet = getAllocatedBooleanSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos);
377 //5 -> 4 for header + 1 for item count
378 iOutLen = 5 + iRows + iItemCount;
382 iOutLen += iItemCount * (iComplex + 1) * sizeof(double) / sizeof(int);
385 piOut = (int *)MALLOC(iOutLen * sizeof(int));
387 piOut[0] = _bData ? sci_sparse : sci_boolean_sparse;
391 piOut[4] = iItemCount;
393 memcpy(piOut + 5, piRowCount, iRows * sizeof(int));
394 memcpy(piOut + 5 + iRows, piColPos, iItemCount * sizeof(int));
399 double *pRealData = (double *)(piOut + 5 + iRows + iItemCount);
401 C2F(dcopy) (&iItemCount, pdblR, &iOne, pRealData, &iOne);
404 double *pImgData = pRealData + iItemCount;
406 C2F(dcopy) (&iItemCount, pdblI, &iOne, pImgData, &iOne);
411 *_piBufferSize = iOutLen;
417 freeAllocatedSparseMatrix(piRowCount, piColPos, pdblR);
421 freeAllocatedComplexSparseMatrix(piRowCount, piColPos, pdblR, pdblI);
426 freeAllocatedBooleanSparse(piRowCount, piColPos);
432 int serialize_to_mpi(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
435 SciErr sciErr = getVarType(_pvCtx, _piAddr, &iType);
438 printError(&sciErr, 0);
445 return serialize_double(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
448 return serialize_string(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
451 return serialize_boolean(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
454 return serialize_sparse(_pvCtx, _piAddr, _piBuffer, _piBufferSize, TRUE);
456 case sci_boolean_sparse:
457 return serialize_sparse(_pvCtx, _piAddr, _piBuffer, _piBufferSize, FALSE);
460 return serialize_int(_pvCtx, _piAddr, _piBuffer, _piBufferSize);