Пример #1
0
/**
  * Gets initial reference point to measure time from (I assume that MPI_Wtime can be skewed).
  */
void profiler_init(const char *name)
{
    profFile = fopen(name, "wt");	// Opens output file.
    if(!profFile) error("profiler_init: cannot open output file stream.");

    time_get(&profStartTime);
    double res, acc;
    res = time_resolution(&acc);
    fprintf(profFile, "Accuracy of the timer is %e sec +/- %e, start time = %e.\n", res, acc, time_seconds(&profStartTime));
    fprintf(profFile, "Name: (group %%) (execution time) (main loop %%)\n");

    profLevel = -1;		// Sets initial level to "main loop".
    profEvent = profEvents;
    profAccumEvent = 0;

    if(atexit(profiler_exit))	// Submits deallocator (to close file anyway).
        error("profiler_init: cannot submit deallocator.");

    memset(profEvents, 0, mc_eventCacheN * sizeof(sample_t));	// Cleans all accumulated values.

    profLongestName = 0;
    for(int bind = 0; bind < mc_prof_N; ++bind)	// Updates precalcs for formatted output.
    {
        profBinds[bind].nameLenght = strlen(profBinds[bind].name);
        profLongestName = (profLongestName < profBinds[bind].nameLenght) ? profBinds[bind].nameLenght : profLongestName;
    }
}
/**
 * \brief Build the list of parameters that configure the tres::Network
 *
 * The tres::Network class (or, more precisely, its underlying concrete
 * implementation) needs to be configured with a bunch of information, such
 * as the description of the message set, the network topology, initialization
 * informations and time resolution, to name only a few. This information is
 * stored inside the T-Res Network block as mask parameters.
 *
 * This function reads the mask parameters and returns a vector-of-string-based
 * description of the information needed by the tres::Network class.
 * Specifically, the information returned by this function has the following form:
 *  - the number of messages in the message-set (#msgs)    - std::string (1)   <-- vector.begin()
 *  - the message-set description                          - std::string (#msgs)
 *      - "message_type;message_UID;"
 *  - the number of items describing the network (#ndescr) - std::string (1)
 *  - the network description                              - std::string (#ndescr)
 *      - "description_type;description_file_paths"
 *  - path to additional libraries for the simulation      - std::string (1)
 *  - the time resolution                                  - std::string (1)
 *                                                                             <-- vector.end()
 *
 * No err checking is performed when reading mask parameters since these are already
 * guaranteed to be valid at MATLAB level (err checking performed by mask callbacks).
 */
static std::vector<std::string> readMaskAndBuildConfVector(SimStruct *S)
{
    char *bufMsgDescr,  // MSG_DESCR_VARNAME
         *bufTimeRes,   // TIME_RESOLUTION
         *bufNtwkDescr, // NTWK_DESCR_VARNAME
         *bufAddLibr;   // OTHER_DEPS

    int bufMsgDescrLen, bufTimeResLen, bufNtwkDescrLen, bufAddLibrLen;
    std::vector<std::string> net_params; // The return list of parameters
    std::stringstream ss;                // A convenience stringstream

    // Get the number of messages (it is equal to the size of output port)
    int_T num_msgs = ssGetOutputPortWidth(S,0);

    // Get the name of the workspace variable for the message set
    bufMsgDescrLen = mxGetN( ssGetSFcnParam(S,MSG_DESCR_VARNAME) )+1;
    bufMsgDescr = new char[bufMsgDescrLen];
    mxGetString(ssGetSFcnParam(S,MSG_DESCR_VARNAME), bufMsgDescr, bufMsgDescrLen);

    // Get the actual message set description
    std::vector<std::string> msg_descr = cellArrayDescrToVectorOfStrings(mexGetVariablePtr("base", bufMsgDescr));
    delete bufMsgDescr;

    // **Insert** the number of messages and
    //            the message set description in the return list
    ss << num_msgs;
    net_params.push_back(ss.str());
    ss.str(std::string());                    // Flush the ss
    net_params.insert(net_params.end(),
                           msg_descr.begin(),
                               msg_descr.end());

    // Get the name of the workspace variable for the Network description
    bufNtwkDescrLen = mxGetN( ssGetSFcnParam(S,NTWK_DESCR_VARNAME) )+1;
    bufNtwkDescr = new char[bufNtwkDescrLen];
    mxGetString(ssGetSFcnParam(S,NTWK_DESCR_VARNAME), bufNtwkDescr, bufNtwkDescrLen);

    // Get the number of Network description entries and
    // the actual Network description
    int_T num_ndescr = mxGetM(mexGetVariablePtr("base", bufNtwkDescr));
    std::vector<std::string> ntwk_descr = cellArrayDescrToVectorOfStrings(mexGetVariablePtr("base", bufNtwkDescr));
    delete bufNtwkDescr;

    // **Insert** the number of Network description entries and
    //            the Network description in the return list
    ss << num_ndescr;
    net_params.push_back(ss.str());
    ss.str(std::string());                    // Flush the ss
    net_params.insert(net_params.end(),
                           ntwk_descr.begin(),
                               ntwk_descr.end());

    // Get the path to additional libraries
    bufAddLibrLen = mxGetN( ssGetSFcnParam(S,OTHER_DEPS) )+1;
    bufAddLibr = new char[bufAddLibrLen];
    mxGetString(ssGetSFcnParam(S,OTHER_DEPS), bufAddLibr, bufAddLibrLen);

    // **Insert** the additional libraries in the return list
    std::string add_libs(bufAddLibr);
    net_params.push_back(add_libs);

    // And finally, get the time resolution
    bufTimeResLen = mxGetN( ssGetSFcnParam(S,TIME_RESOLUTION) )+1;
    bufTimeRes = new char[bufTimeResLen];
    mxGetString(ssGetSFcnParam(S,TIME_RESOLUTION), bufTimeRes, bufTimeResLen);

    // Convert the time resolution to a double
    std::string time_resolution(bufTimeRes);
    delete bufTimeRes;
    if (time_resolution == "Seconds")
        ss << 1.0;
    else if (time_resolution == "Milli_Seconds")
        ss << 1.0e3;
    else if (time_resolution == "Micro_Seconds")
        ss << 1.0e6;
    else if (time_resolution == "Nano_Seconds")
        ss << 1.0e9;

    // **Insert** the time resolution in the return list
    net_params.push_back(ss.str());
    ss.str(std::string());                    // Flush the ss

    // Done, return to the caller
    return (net_params);
}