Changes between Version 5 and Version 6 of C++ API

Show
Ignore:
Timestamp:
09/10/12 18:42:43 (12 years ago)
Author:
jorisborgdorff
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • C++ API

    v5 v6  
    9999{{{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. 
    100100 
    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 
     103In 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 
    102104{{{ 
    103105#include <vector> 
     
    117119    } 
    118120    vector<int> dims = cdata->getDimensions(); 
     121 
     122    // get the matrix as a vector 
    119123    double *data = (double *) cdata->getData(); 
     124 
     125    // Do something with the data 
    120126    double result = 0; 
    121127    for (int x = 0; x < dims[0]; x++) { 
    122128        for (int y = 0; y < dims[1]; y++) { 
     129           // the matrix is indexed according to the different dimensions 
    123130           result += data[cdata->fidx(x,y)]; 
    124131        } 
    125132    } 
     133    muscle::env::free_data(cdata, MUSCLE_COMPLEX); 
    126134    return result; 
    127135} 
    128136}}} 
     137The 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{{{ 
     139vector<int> dims(2); 
     140// Set dimensions x and y 
     141dims[0] = 10; dims[1] = 14; 
     142ComplexData cdata(COMPLEX_DOUBLE_MATRIX_2D, &dims); 
     143double *data = (double *)cdata.getData(); 
     144 
     145// Do something with the data 
     146double result = 0; 
     147for (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} 
     153muscle::env::send("matrixOut", &cdata, cdata.length(), MUSCLE_COMPLEX); 
     154}}}