Changes between Version 5 and Version 6 of C++ API
- Timestamp:
- 09/10/12 18:42:43 (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
C++ API
v5 v6 99 99 {{{init}}}, {{{finalize}}}, {{{will_stop}}}, {{{send}}}, and {{{receive}}} behave exactly as their C counterparts. Since scientific data is often vectors of doubles, there are two new convenience functions to send or receive only double vectors. Vectors are safer than arrays memory-wise and they do range-checking. If other vector methods are of interest to you, send us [[mailto:j.borgdorff@uva.nl|an email]]. Except for vectors, the data that is received must be freed by MUSCLE by calling {{{free_data}}} with the received pointer and the datatype of the received pointer. 100 100 101 In addition to the datatypes that are available in the C API, the C++ API can also receive a {{{MUSCLE_COMPLEX}}} type. This will receive a {{{muscle::ComplexData}}} object, defined in the {{{muscle2/complex_data.hpp}}} header file. This ComplexData is used when the sender of a message sends different data than an array. In the enum {{{muscle_complex_t}}} all possible types are listed. For the moment only arrays and matrices are supported. Working with ComplexData goes as follows, for example with a two-dimensional double array 101 === Complex data === 102 103 In addition to the datatypes that are available in the C API, the C++ API can also receive a {{{MUSCLE_COMPLEX}}} type. This will receive a {{{muscle::ComplexData}}} object, defined in the {{{muscle2/complex_data.hpp}}} header file. This ComplexData is used when the sender of a message sends different data than an array. A Java sender might send a 2-D array of doubles, for instance. In the enum {{{muscle_complex_t}}} all possible types are listed. For the moment only arrays and matrices are supported. Working with ComplexData goes as follows, for example with a two-dimensional double array 102 104 {{{ 103 105 #include <vector> … … 117 119 } 118 120 vector<int> dims = cdata->getDimensions(); 121 122 // get the matrix as a vector 119 123 double *data = (double *) cdata->getData(); 124 125 // Do something with the data 120 126 double result = 0; 121 127 for (int x = 0; x < dims[0]; x++) { 122 128 for (int y = 0; y < dims[1]; y++) { 129 // the matrix is indexed according to the different dimensions 123 130 result += data[cdata->fidx(x,y)]; 124 131 } 125 132 } 133 muscle::env::free_data(cdata, MUSCLE_COMPLEX); 126 134 return result; 127 135 } 128 136 }}} 137 The indices of the 2-D array are retrieved using the {{{index}}} or {{{fidx}}} function, where the latter is faster but does not do range or dimensionality checking. To send a ComplexData object it must first be constructed. The easiest way is to let ComplexData do the memory allocation and destruction, and getting the allocated memory from ComplexData to perform actual actions with. 138 {{{ 139 vector<int> dims(2); 140 // Set dimensions x and y 141 dims[0] = 10; dims[1] = 14; 142 ComplexData cdata(COMPLEX_DOUBLE_MATRIX_2D, &dims); 143 double *data = (double *)cdata.getData(); 144 145 // Do something with the data 146 double result = 0; 147 for (int x = 0; x < dims[0]; x++) { 148 for (int y = 0; y < dims[1]; y++) { 149 // the matrix is indexed according to the different dimensions, using fidx 150 result += data[cdata.fidx(x,y)]; 151 } 152 } 153 muscle::env::send("matrixOut", &cdata, cdata.length(), MUSCLE_COMPLEX); 154 }}}