Changes between Version 4 and Version 5 of C++ API
- Timestamp:
- 09/10/12 14:00:45 (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
C++ API
v4 v5 3 3 == C API == 4 4 5 All muscle2 headers are installed in {{{$MUSCLE_HOME/include/muscle2}}}. The header f or the C API is {{{muscle2/cmuscle.h}}}.5 All muscle2 headers are installed in {{{$MUSCLE_HOME/include/muscle2}}}. The header file for the C API is {{{muscle2/cmuscle.h}}}. 6 6 7 7 The functions there are very limited, but they should be sufficient for most needs: … … 41 41 The type {{{MUSCLE_COMPLEX}}} is only available for C++ code. 42 42 43 Receiving a message can be done in two ways: either the memory is initialized beforehand and the buffer size is given as the third argument, or a 0pointer is passed, in which case MUSCLE will allocate the memory. In both cases, the memory must be freed by the user. For example:43 Receiving a message can be done in two ways: either the memory is initialized beforehand and the number of elements in the array is given as the third argument, or a 0-pointer is passed, in which case MUSCLE will allocate the memory. In both cases, the memory must be freed by the user. For example: 44 44 {{{ 45 45 size_t sz = 100; … … 64 64 } 65 65 }}} 66 The first example will only work if you know the upper bound of received message size in advance. In this way, memory is only allocated once per submodel run. The second example is safer since MUSCLE will allocate the necessary data for you based on the message size. It does mean that new memory is allocatedeach message.66 The first example will only work if the upper bound of received message size is known in advance. The second example is safer since MUSCLE will allocate the necessary data for you based on the message size. However, it also means that new memory is allocated for each message. 67 67 68 The {{{MUSCLE_Send}}} command can only be used one way .68 The {{{MUSCLE_Send}}} command can only be used one way, and again starts with the conduit entrance name, then the data, the number of elements in the array and finally the type of data. 69 69 {{{ 70 70 double data[100]; … … 80 80 == C++ API == 81 81 82 The C++ API resembles the C API but it is more extensive. Relevant header files are {{{muscle2/cppmuscle.hpp}}} and {{{muscle2/complex_data.hpp}}}. All functions included {{{cppmuscle.hpp}}} are static, so the API can only be used for one submodel per executable. The following functions are available: 83 {{{ 84 muscle_error_t muscle::env::init(int* argc, char ***argv); 85 void muscle::env::finalize(void); 82 86 87 bool muscle::env::will_stop(void); 88 89 void muscle::env::send(std::string entrance_name, const void *data, size_t count, muscle_datatype_t type); 90 void muscle::env::sendDoubleVector(std::string entrance_name, const std::vector<double>& data); 91 92 void* muscle::env::receive(std::string exit_name, void *data, size_t &count, muscle_datatype_t type); 93 std::vector<double> muscle::env::receiveDoubleVector(std::string exit_name); 94 95 void muscle::env::free_data(void *ptr, muscle_datatype_t type); 96 97 std::string muscle::env::get_tmp_path(void); 98 }}} 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 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 102 {{{ 103 #include <vector> 104 #include <stdexcept> 105 #include <muscle2/cppmuscle.hpp> 106 #include <muscle2/complex_data.hpp> 107 108 using namespace std; 109 using namespace muscle; 110 111 double processMessage() 112 { 113 size_t sz; 114 ComplexData *cdata = (ComplexData *) muscle::env::receive("matrixIn", (void *)0, &sz, MUSCLE_COMPLEX); 115 if (cdata->getType() != COMPLEX_DOUBLE_MATRIX_2D) { 116 throw new runtime_error("Expecting double 2D matrix; received something else."); 117 } 118 vector<int> dims = cdata->getDimensions(); 119 double *data = (double *) cdata->getData(); 120 double result = 0; 121 for (int x = 0; x < dims[0]; x++) { 122 for (int y = 0; y < dims[1]; y++) { 123 result += data[cdata->fidx(x,y)]; 124 } 125 } 126 return result; 127 } 128 }}}