1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/nodes/opal.cpp

367 lines
10 KiB
C++
Raw Permalink Normal View History

/** Node type: OPAL (AsyncApi)
*
* This file implements the opal subtype for nodes.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2015-06-02 21:53:04 +02:00
*********************************************************************************/
2021-02-24 09:17:53 +01:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#include <cmath>
2021-02-24 09:17:53 +01:00
#include <vector>
#include <villas/node_compat.hpp>
#include <villas/nodes/opal.hpp>
#include <villas/utils.hpp>
2021-02-24 09:17:53 +01:00
#include <villas/exceptions.hpp>
extern "C" {
/* Define RTLAB before including OpalPrint.h for messages to be sent
* to the OpalDisplay. Otherwise stdout will be used. */
#define RTLAB
#include <OpalPrint.h>
#include <AsyncApi.h>
#include <OpalGenAsyncParamCtrl.h>
}
/* Private static storage */
2021-02-24 09:17:53 +01:00
static std::string asyncShmemName; /**< Shared Memory identifiers and size, provided via argv. */
static std::string printShmemName; /**< Shared Memory identifiers and size, provided via argv. */
static size_t asyncShmemSize; /**< Shared Memory identifiers and size, provided via argv. */
static std::vector<unsigned> sendIDs, recvIDs; /** A dynamically allocated array of SendIDs. */
static Opal_GenAsyncParam_Ctrl params; /** String and Float parameters, provided by the OPAL AsyncProcess block. */
static pthread_mutex_t lock; /** Big Global Lock for libOpalAsync API */
using namespace villas;
using namespace villas::utils;
2021-09-15 17:09:11 +02:00
/** A bunch of symbols which are used by the libOpal libraries
* and undefined by GCC. We replace them by GCC variants here.
*/
2021-02-24 09:17:53 +01:00
extern "C" {
int __xstat(int ver, const char * path, struct stat * stat_buf)
{
return stat(path, stat_buf);
}
int backtrace(void **buffer, int size)
{
return 0;
}
char **backtrace_symbols(void *const *buffer, int size)
{
return nullptr;
}
void backtrace_symbols_fd(void *const *buffer, int size, int fd)
{
}
2021-02-24 09:17:53 +01:00
void * _intel_fast_memset(void *b, int c, size_t len)
{
return memset(b, c, len);
}
2021-02-24 09:17:53 +01:00
void * _intel_fast_memcpy(void *dst, const void *src, size_t n)
{
return memcpy(dst, src, n);
}
2021-02-24 09:17:53 +01:00
int _intel_fast_memcmp(const void *s1, const void *s2, size_t n)
{
return memcmp(s1, s2, n);
}
2021-09-15 17:09:11 +02:00
void * _intel_fast_memmove(void *s1, const void *s2, size_t n)
{
return memmove(s1, s2, n);
}
2021-02-24 09:17:53 +01:00
}
2015-06-02 22:04:03 +02:00
static
int opal_register_region(int argc, char *argv[])
{
2021-09-15 17:09:11 +02:00
if (argc != 4)
return -1;
2021-02-24 09:17:53 +01:00
asyncShmemName = argv[1];
asyncShmemSize = atoi(argv[2]);
printShmemName = argv[3];
return 0;
}
int villas::node::opal_type_start(villas::node::SuperNode *sn)
2015-12-11 17:56:14 +01:00
{
2021-02-24 09:17:53 +01:00
int err, noRecvIcons, noSendIcons;
2015-08-07 01:11:43 +02:00
2019-04-22 23:45:38 +02:00
pthread_mutex_init(&lock, nullptr);
2015-08-07 01:11:43 +02:00
/* Enable the OpalPrint function. This prints to the OpalDisplay. */
2021-02-24 09:17:53 +01:00
err = OpalSystemCtrl_Register((char *) printShmemName.c_str());
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("OpalPrint() access not available ({})", err);
2015-08-07 01:11:43 +02:00
/* Open Share Memory created by the model. */
2021-02-24 09:17:53 +01:00
err = OpalOpenAsyncMem(asyncShmemSize, asyncShmemName.c_str());
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Model shared memory not available ({})", err);
err = OpalGetAsyncCtrlParameters(&params, sizeof(Opal_GenAsyncParam_Ctrl));
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Could not get OPAL controller parameters ({})", err);
2015-08-07 01:11:43 +02:00
/* Get list of Send and RecvIDs */
2021-02-24 09:17:53 +01:00
err = OpalGetNbAsyncSendIcon(&noSendIcons);
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to get number of send blocks ({})", err);
2021-02-24 09:17:53 +01:00
err = OpalGetNbAsyncRecvIcon(&noRecvIcons);
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to get number of recv blocks ({})", err);
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
sendIDs.resize(noSendIcons);
recvIDs.resize(noRecvIcons);
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
err = OpalGetAsyncSendIDList(sendIDs.data(), noSendIcons * sizeof(int));
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to get list of send ids ({})", err);
2021-02-24 09:17:53 +01:00
err = OpalGetAsyncRecvIDList(recvIDs.data(), noRecvIcons * sizeof(int));
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to get list of recv ids ({})", err);
2015-08-07 01:11:43 +02:00
2021-09-13 14:54:26 +02:00
auto logger = logging.get("node:opal");
logger->info("Started as OPAL Asynchronous process");
logger->info("This is VILLASnode %s (built on %s, %s)",
PROJECT_BUILD_ID, __DATE__, __TIME__);
2015-09-22 13:20:20 +02:00
opal_print_global();
return 0;
}
int villas::node::opal_type_stop()
2015-12-11 17:56:14 +01:00
{
2015-03-17 22:59:43 +01:00
int err;
2021-02-24 09:17:53 +01:00
err = OpalCloseAsyncMem(asyncShmemSize, asyncShmemName.c_str());
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to close shared memory area ({})", err);
2015-08-07 01:11:43 +02:00
2021-02-16 14:15:14 +01:00
auto logger = logging.get("node:opal");
logger->debug("Closing OPAL shared memory mapping");
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
err = OpalSystemCtrl_UnRegister((char *) printShmemName.c_str());
2015-09-19 15:28:28 +02:00
if (err != EOK)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Failed to close shared memory for system control ({})", err);
2015-08-07 01:11:43 +02:00
pthread_mutex_destroy(&lock);
return 0;
}
static
int opal_print_global()
2015-12-11 17:56:14 +01:00
{
2021-02-16 14:15:14 +01:00
auto logger = logging.get("node:opal");
logger->debug("Controller ID: {}", params.controllerID);
2021-02-24 09:17:53 +01:00
std::stringstream sss, rss;
2021-02-24 09:17:53 +01:00
for (auto i : sendIDs)
sss << i << " ";
for (auto i : recvIDs)
rss << i << " ";
2015-08-07 01:11:43 +02:00
2021-02-16 14:15:14 +01:00
logger->debug("Send Blocks: {}", sss.str());
logger->debug("Receive Blocks: {}", rss.str());
2021-02-24 09:17:53 +01:00
2021-02-16 14:15:14 +01:00
logger->debug("Control Block Parameters:");
2015-10-06 09:10:08 +02:00
for (int i = 0; i < GENASYNC_NB_FLOAT_PARAM; i++)
2021-09-13 14:54:26 +02:00
logger->debug("FloatParam[{}] = {}", i, (double) params.FloatParam[i]);
2015-10-06 09:10:08 +02:00
for (int i = 0; i < GENASYNC_NB_STRING_PARAM; i++)
2021-02-16 14:15:14 +01:00
logger->debug("StringParam[{}] = {}", i, params.StringParam[i]);
2015-08-07 01:11:43 +02:00
return 0;
}
int villas::node::opal_parse(NodeCompat *n, json_t *json)
{
auto *o = n->getData<struct opal>();
2015-08-07 01:11:43 +02:00
int ret;
json_error_t err;
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: i, s: i, s: b }",
"send_id", &o->sendID,
"recv_id", &o->sendID,
"reply", &o->reply
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-opal");
return 0;
}
char * villas::node::opal_print(NodeCompat *n)
{
auto *o = n->getData<struct opal>();
2015-08-07 01:11:43 +02:00
2021-07-09 19:04:46 +02:00
/** @todo Print send_params, recv_params */
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
return strf("sendID=%u, recvID=%u, reply=%u",
o->sendID, o->recvID, o->reply);
}
int villas::node::opal_start(NodeCompat *n)
{
auto *o = n->getData<struct opal>();
2015-08-07 01:11:43 +02:00
/* Search for valid send and recv ids */
int sfound = 0, rfound = 0;
2021-02-24 09:17:53 +01:00
for (auto i : sendIDs)
sfound += i == o->sendID;
for (auto i : recvIDs)
rfound += i == o->sendID;
2015-08-07 01:11:43 +02:00
if (!sfound)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid send_id '{}'", o->sendID);
if (!rfound)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid recv_id '{}'", o->recvID);
2015-08-07 01:11:43 +02:00
/* Get some more informations and paramters from OPAL-RT */
2021-02-24 09:17:53 +01:00
OpalGetAsyncSendIconMode(&o->mode, o->sendID);
OpalGetAsyncSendParameters(&o->sendParams, sizeof(Opal_SendAsyncParam), o->sendID);
OpalGetAsyncRecvParameters(&o->recvParams, sizeof(Opal_RecvAsyncParam), o->recvID);
2021-02-24 09:17:53 +01:00
o->sequenceNo = 0;
return 0;
}
int villas::node::opal_read(NodeCompat *n, struct Sample * const smps[], unsigned cnt)
{
auto *o = n->getData<struct opal>();
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
int state, ret, len;
unsigned id;
2015-08-07 01:11:43 +02:00
struct Sample *s = smps[0];
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
double data[s->capacity];
2015-10-13 16:14:01 +02:00
if (cnt != 1)
2021-02-16 14:15:14 +01:00
throw RuntimeError("The OPAL-RT node type does not support combining!");
2015-08-07 01:11:43 +02:00
/* This call unblocks when the 'Data Ready' line of a send icon is asserted. */
do {
2015-09-19 15:28:28 +02:00
ret = OpalWaitForAsyncSendRequest(&id);
if (ret != EOK) {
state = OpalGetAsyncModelState();
2021-02-24 09:17:53 +01:00
if ((state == STATE_RESET) || (state == STATE_STOP))
2021-02-16 14:15:14 +01:00
throw RuntimeError("OpalGetAsyncModelState(): Model stopped or resetted!");
return -1; /* @todo correct return value */
}
2021-02-24 09:17:53 +01:00
} while (id != o->sendID);
/* No errors encountered yet */
2021-02-24 09:17:53 +01:00
OpalSetAsyncSendIconError(0, o->sendID);
/* Get the size of the data being sent by the unblocking SendID */
2021-02-24 09:17:53 +01:00
OpalGetAsyncSendIconDataLength(&len, o->sendID);
if ((unsigned) len > s->capacity * sizeof(s->data[0])) {
2021-02-16 14:15:14 +01:00
n->logger->warn("Ignoring the last {} of {} values for OPAL (send_id={}).",
2021-09-13 14:54:26 +02:00
len / sizeof(double) - s->capacity, len / sizeof(double), o->sendID);
len = sizeof(data);
}
/* Read data from the model */
2021-02-24 09:17:53 +01:00
OpalGetAsyncSendIconData(data, len, o->sendID);
2021-02-24 09:17:53 +01:00
s->sequence = htons(o->sequenceNo++);
s->length = (unsigned) len / sizeof(double);
2021-02-24 09:17:53 +01:00
for (unsigned i = 0; i < s->length; i++)
s->data[i].f = (float) data[i]; /* OPAL provides double precission */
/* This next call allows the execution of the "asynchronous" process
* to actually be synchronous with the model. To achieve this, you
* should set the "Sending Mode" in the Async_Send block to
* NEED_REPLY_BEFORE_NEXT_SEND or NEED_REPLY_NOW. This will force
* the model to wait for this process to call this
* OpalAsyncSendRequestDone function before continuing. */
if (o->reply)
2021-02-24 09:17:53 +01:00
OpalAsyncSendRequestDone(o->sendID);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
state = OpalGetAsyncModelState();
2021-02-24 09:17:53 +01:00
if ((state == STATE_RESET) || (state == STATE_STOP))
2021-02-16 14:15:14 +01:00
throw RuntimeError("OpalGetAsyncModelState(): Model stopped or resetted!");
2015-05-07 13:57:43 +02:00
return 1;
}
int villas::node::opal_write(NodeCompat *n, struct Sample * const smps[], unsigned cnt)
{
auto *o = n->getData<struct opal>();
2015-08-07 01:11:43 +02:00
struct Sample *s = smps[0];
2015-08-07 01:11:43 +02:00
int state;
int len;
2021-02-24 09:17:53 +01:00
double data[s->length];
2015-10-13 16:14:01 +02:00
if (cnt != 1)
2021-02-16 14:15:14 +01:00
throw RuntimeError("The OPAL-RT node type does not support combining!");
2015-08-07 01:11:43 +02:00
state = OpalGetAsyncModelState();
2021-02-24 09:17:53 +01:00
if ((state == STATE_RESET) || (state == STATE_STOP))
2021-02-16 14:15:14 +01:00
throw RuntimeError("OpalGetAsyncModelState(): Model stopped or resetted!");
2021-02-24 09:17:53 +01:00
OpalSetAsyncRecvIconStatus(s->sequence, o->recvID); /* Set the Status to the message ID */
OpalSetAsyncRecvIconError(0, o->recvID); /* Set the Error to 0 */
/* Get the number of signals to send back to the model */
2021-02-24 09:17:53 +01:00
OpalGetAsyncRecvIconDataLength(&len, o->recvID);
2021-09-13 14:54:26 +02:00
if (len > (int) sizeof(data))
2021-02-16 14:15:14 +01:00
n->logger->warn("Node expecting more signals ({}) than values in message ({})", len / sizeof(double), s->length);
2015-08-07 01:11:43 +02:00
2021-02-24 09:17:53 +01:00
for (unsigned i = 0; i < s->length; i++)
data[i] = (double) s->data[i].f; /* OPAL expects double precission */
2021-02-24 09:17:53 +01:00
OpalSetAsyncRecvIconData(data, s->length * sizeof(double), o->recvID);
2015-08-07 01:11:43 +02:00
2015-05-07 13:57:43 +02:00
return 1;
}
static NodeCompatType p;
__attribute__((constructor(110)))
static void register_plugin() {
2021-06-21 16:11:42 -04:00
p.name = "opal";
p.description = "run as OPAL Asynchronous Process (libOpalAsyncApi)";
p.vectorize = 1;
p.size = sizeof(struct opal);
2021-09-13 14:54:26 +02:00
p.type.start = opal_type_start;
p.type.stop = opal_type_stop;
2021-06-21 16:11:42 -04:00
p.parse = opal_parse;
p.print = opal_print;
p.start = opal_start;
p.read = opal_read;
p.write = opal_write;
static NodeCompatFactory ncp(&p);
}