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

Show
Ignore:
Timestamp:
09/10/12 14:00:45 (12 years ago)
Author:
jorisborgdorff
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • C++ API

    v4 v5  
    33== C API == 
    44 
    5 All muscle2 headers are installed in {{{$MUSCLE_HOME/include/muscle2}}}. The header for the C API is {{{muscle2/cmuscle.h}}}. 
     5All muscle2 headers are installed in {{{$MUSCLE_HOME/include/muscle2}}}. The header file for the C API is {{{muscle2/cmuscle.h}}}. 
    66 
    77The functions there are very limited, but they should be sufficient for most needs: 
     
    4141The type {{{MUSCLE_COMPLEX}}} is only available for C++ code. 
    4242 
    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 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: 
     43Receiving 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: 
    4444{{{ 
    4545size_t sz = 100; 
     
    6464} 
    6565}}} 
    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 allocated each message. 
     66The 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. 
    6767 
    68 The {{{MUSCLE_Send}}} command can only be used one way. 
     68The {{{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. 
    6969{{{ 
    7070double data[100]; 
     
    8080== C++ API == 
    8181 
     82The 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{{{ 
     84muscle_error_t muscle::env::init(int* argc, char ***argv); 
     85void muscle::env::finalize(void); 
    8286 
     87bool muscle::env::will_stop(void); 
     88 
     89void muscle::env::send(std::string entrance_name, const void *data, size_t count, muscle_datatype_t type); 
     90void muscle::env::sendDoubleVector(std::string entrance_name, const std::vector<double>& data); 
     91 
     92void* muscle::env::receive(std::string exit_name, void *data, size_t &count, muscle_datatype_t type); 
     93std::vector<double> muscle::env::receiveDoubleVector(std::string exit_name); 
     94 
     95void muscle::env::free_data(void *ptr, muscle_datatype_t type); 
     96 
     97std::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 
     101In 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 
     108using namespace std; 
     109using namespace muscle; 
     110 
     111double 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}}}