message synchronization with boost::mpi send/recv? - mpi

I call mpirun with "-np 2". I'm referring to the process with rank 0 as "master" and the process with rank 1 as "slave".
Goal:
master occasionally sends a message to slave such as mpi::send(1, UPDATE, data);. Other message types include DIE, COMPUTE ...etc. Those message types are constant integers with unique values.
slave runs an infinite loop, "listening" to any message from the master. When it receives a message, it sends an acknowledgement back to the master.
Implementation:
slave runs:
...
int updateData, computeData;
mpi::request updateRequest = world.irecv(0,UPDATE, updateData);
mpi::request computeRequest = world.irecv(0,COMPUTE, computeData);
do {
cerr << "slave ready to take a command" << endl;
if(updateRequest.test()) {
cerr << "slave ireceived UPDATE" << endl;
world.send(0, UPDATE_ACK, 0);
cerr << "slave sent UPDATE_ACK" << endl;
/* do something useful
...
...
*/
updateRequest = world.irecv(0, UPDATE, updateData);
} else if (computeRequest.test()) {
...
} else {
boost::this_thread::sleep( boost::posix_time::seconds(1) );
}
}
while the master runs:
...
world.send(1, UPDATE, 10);
cerr << "master sent UPDATE" << endl;
int dummy;
world.recv(1, UPDATE_ACK, dummy);
cerr << "master received UPDATE_ACK" << endl;
...
more context for the master's code:
...
// update1
world.send(1, UPDATE, params);
cerr << "master sent UPDATE" << endl;
int dummy;
world.recv(1, UPDATE_ACK, dummy);
cerr << "master received UPDATE_ACK" << endl;
// update2
world.send(1, UPDATE2, params2);
cerr << "master sent UPDATE2" << endl;
world.recv(1, UPDATE2_ACK, dummy);
cerr << "master received UPDATE2_ACK" << endl;
// update3
world.send(1, UPDATE3, params3);
cerr << "master sent UPDATE3" << endl;
world.recv(1, UPDATE3_ACK, dummy);
cerr << "master received UPDATE3_ACK" << endl;
...
// training iterations
do {
mpi::request computeRecvReq1, computeRecvReq2;
std::map<int, int> result1, result2;
// for each line in a text file, the master asks the slave(s)
// to compute two things and aggregates the results
for(unsigned sentId = 0; sentId != data.size(); sentId++) {
// these two functions won't return until at least one slave is "idle"
CollectSlavesWork1(computeRecvReq1, result1);
CollectSlavesWork2(computeRecvReq2, result2);
// async ask the slave to compute and async get the results
world.isend(1, COMPUTE, sentId);
computeRecvReq1 = world.irecv(1, RESULT1, result1);
computeRecvReq2 = world.irecv(1, RESULT2, result2);
}
// based on the slave(s) work, the master updates params1
// and send them again to the slave(s)
world.send(1, UPDATE, params);
cerr << "master sent UPDATE" << endl;
world.recv(1, UPDATE_ACK, dummy); // PROBLEM HAPPENS HERE
cerr << "master received UPDATE_ACK" << endl;
} while(!ModelIsConverged())
...
Output:
...
slave ready to take a command
master sent UPDATE
slave ireceived UPDATE
slave sent UPDATE_ACK
master received UPDATE_ACK
slave ready to take a command
...
slave ready to take a command
master sent UPDATE
slave ireceived UPDATE
slave sent UPDATE_ACK
slave ready to take a command
...
Problem:
the first time the master sends an UPDATE message everything seems to be alright. However, in the second time, the master doesn't receive the UPDATE_ACK.

Related

QSerialPort detecting lost connection

My setup is as follows: Arduino Mega UART3 <--> Voltage Level shifter <--> Rpi4
All the code can be found here
Data is being sent synchronously from the Arduino to the Rpi at 100ms and 1000ms (depending on the importance of the data). And also data is being sent from the Rpi back to the Arduino asynchronously on GUI input (There is also a sync sent at 1000ms in case some frames are lost/corrupted).
Coms are handled in a separate thread than the GUI to provide the best user experience.
Well, all is working fine but I have been trying unsuccessfully to detect if the Rpi has lost connection with the Arduino. (In the other direction is covered with a serial watchdog.)
I wanted to avoid the use of another watchdog in the Qt part but I cannot find a suitable signal to detect the lost serial connection as the QSerialPort keeps reporting a Timeout error randomly... and there are no other methods that detect successfully the disconnection.
Here below is the workaround that I am trying to use without much success: (Probably it needs a major rewrite... but I need to be pointed in the right direction first)
// Serial Port Initialization
m_Serial = new QSerialPort();
m_Serial->setPortName("ttyS0");
//m_Serial->setPortName("ttyUSB0");
m_Serial->setBaudRate(QSerialPort::Baud115200);
m_Serial->setDataBits(QSerialPort::Data8);
m_Serial->setParity(QSerialPort::NoParity);
m_Serial->setStopBits(QSerialPort::OneStop);
m_Serial->setFlowControl(QSerialPort::NoFlowControl);
m_Serial->open(QIODevice::ReadWrite);
qDebug() << "SerialPort Status: " << m_Serial->isOpen();
emit serialConnected(m_Serial->isReadable());
while(!abort)
{
QThread::msleep(5);
while (!isConected(m_Serial)){
emit serialConnected(false);
isSerialConected = false;
QThread::sleep(1);
qDebug() << "Serial Error: ";
//qDebug() << "Is readable?: " << m_Serial->isReadable();
//qDebug() << "Bytes availiable?: " << m_Serial->bytesAvailable();
mutex.lock();
abort = _abort;
mutex.unlock();
}
if (!isSerialConected){
emit serialConnected(true);
isSerialConected=true;
}
mutex.lock();
abort = _abort;
mutex.unlock();
if(!m_outFrameQueue->isEmpty())
{
//qDebug() << "Frame empty";
{ *sendData*}
} else
{
if (m_Serial->waitForReadyRead(10) )
{ *readData*}
And the function that I am trying to use to detect if is actually connected:
bool SerialWorker::isConected(QSerialPort *m_Serial){
// qDebug() << "SerialPort Error: " << m_Serial->error();
// qDebug() << "SerialPort isReadable: " << m_Serial->isReadable();
// qDebug() << "SerialPort isReadable: " << m_Serial->bytesAvailable();
if (m_Serial->error() == QSerialPort::SerialPortError::NoError){
return true;
}
else if (m_Serial->error() == QSerialPort::SerialPortError::TimeoutError){
m_Serial->clearError();
//m_Serial->reset();
return true;
}else{
m_Serial->reset();
m_Serial->close();
m_Serial->clearError();
QThread::sleep(1);
m_Serial->open(QIODevice::ReadWrite);
return false;
}
}

How to connect using UDP to my pic32 and get an answer from it

I want to connet with Qt on windows to my PIC32 UDP server. With a test program in C, I can connect and get the answer, but with Qt it is impossible. What am I doing wrong ?
As you can see I use an ACTIVE WAITING with the while, and my slot doesn't seems to be triggered. Maybe you can see my mistake here ? I hate active waiting....
Here is my code on Qt :
void MuTweezer::run()
{
QHostAddress sender;
quint16 senderPort;
QByteArray datagram;
qint64 pendingBytes;
int cpt = 0;
// Message to send
QByteArray message("Hello that's charly");
// m_imuRcvSocket.bind(10000); why is it for in a client side !?
// SEEMS to NOT BE TRIGGERED
connect(&m_imuRcvSocket, SIGNAL(readyRead()), this, SLOT(recu()));
while(m_isRunning && cpt++ < 10)
{
// send the initial message
qint64 bytesSent= m_imuRcvSocket.writeDatagram(message, QHostAddress("192.168.0.15"), 10000);
cout << "Bytes sent : " << bytesSent << endl;
// We wait the answer from the server
while(!m_imuRcvSocket.hasPendingDatagrams());
// If there is no datagram available, this function returns -1
if((pendingBytes = m_imuRcvSocket.pendingDatagramSize()) == -1)
continue;
datagram.resize(pendingBytes);
m_imuRcvSocket.readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
cout << "================="
<< "\nMessage from <" << sender.toString().toStdString().substr(7) << "> on port " << senderPort
<< "\nString : " << datagram.data()
<< "\nSize: " << pendingBytes << " Bytes (characters)\n"
<< "=================" <<
endl;
}
}
Here is my code on the PIC32, as you can see, once I receive a message, I send the answer, it allows me to make a bidirectionnal communication :
if(!UDPIsOpened(mySocket)){
DBPRINTF("Socket CLOSED");
continue; // go back to loop beginning
}
DBPRINTF("Socket OPEN");
if(!(lengthToGet = UDPIsGetReady(mySocket)))
continue;
// get the string
// UDPGetArray : returns the number of bytes successfully read from the UDP buffer.
if((lengthWeGot = UDPGetArray(message, lengthToGet)))
UDPDiscard(); // Discards any remaining RX data from a UDP socket.
/* Modifie it and send it back */
if(UDPIsPutReady(mySocket)){
message[20]= 'Z';
message[21]= 'i';
message[22]= 'b';
message[23]= 'o';
UDPPutArray(message, lengthWeGot);
UDPFlush();
}
Any idea ?
Try to use waitForBytesWritten and waitForReadyRead:
// to receive datagrams, the socket needs to be bound to an address and port
m_imuRcvSocket.bind();
// send the initial message
QByteArray message("Hi it's Charly");
qint64 bytesSent= m_imuRcvSocket.writeDatagram(message,
QHostAddress("200.0.0.3"),
10000);
bool datagramWritten = m_imuRcvSocket.waitForBytesWritten();
// add code to check datagramWritten
datagram.resize(50); // random size for testing
bool datagramReady = m_imuRcvSocket.waitForReadyRead() && m_imuRcvSocket.hasPendingDatagrams();
// add code to check datagramReady
m_imuRcvSocket.readDatagram(datagram.data(),
datagram.size(),
&sender,
&senderPort);
cout << "================="
<< "\nMessage from <" << sender << "> on port " << senderPort
<< "\nString : " << datagram
<< "\nSize: " << pendingBytes << " Bytes (characters)\n"
<< "=================" <<
endl;
A better alternative would be to use signals and slots as described in the documentation of QUdpSocket
if you plan to use the microprocessor as a client with UDP you need the MAC address of the destination machine otherwise it will not work. This one took me 4 hours to figure out.

QUdpSocket reading issue

I have an issue when I receive data from a UDP client. The code that I used is:
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress("192.168.1.10"),2000);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
qDebug() << "Socket establert";
}
void MyUDP::HelloUDP()
{
QByteArray Data;
Data.append("R");
socket->writeDatagram(Data, QHostAddress("192.168.1.110"), 5001);
qDebug() << "Enviat datagrama";
}
void MyUDP::readyRead()
{
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << buffer;
qDebug() << "Size: " << buffer.size();
qDebug() << "Pending datagrams: " << socket->hasPendingDatagrams();
QString str(buffer);
QString res = str.toAscii().toHex(); qDebug() << res;
}
The problem is that in Wireshark I receive this data (all the data):
Internet Protocol Version 4, Src: 192.168.1.110, Dst: 192.168.1.10
User Datagram Protocol, Src Port: 5001, Dst Port: 2000
Data (20 bytes)
Data: 58bf80000059bf800000410000000053bf800000
[Length: 20]
But at the console output of my application I receive this trunkated data:
Message from: "192.168.1.110"
Message port: 5001
Message: "X¿
Size: 20
Pending datagrams: false
"58bf80"
You can see that only the first part of data "58bf80" is received. It seems that the datagram no has any limitation, and the socket runs fine. I don't see what may be happening.
Thanks in advance.
The truncation probably happens in conversion from QByteArray to QString, the string getting truncated in null terminator (byte with value 0).
To correctly convert from QByteArray to hex encoded QString use toHex function, like on the following example:
QByteArray data; //The data you got!
QString str = QString(data.toHex()); //Perform the conversion to hex encoded and to string

recv crash without return

I have this piece of code, where I should receive some data over a TCP socket:
int ret;
char mytext[SIZE_CTRL_MSG];
cerr << "Trying to receive" << endl;
ret = recv(sock, &mytext[0], SIZE_CTRL_MSG, 0);
cerr << "Packet received, ret=" << ret << endl;
if(ret == SOCKET_ERROR){
printf("recv failed with error (%d)\n", WSAGetLastError());
system("PAUSE");
exit(1);
}
but when I try to execute it, the application crashes without returning a value; the output is this:
Trying to receive
and here the application crashes. I only receive a message, "Unhandled exception win32 occurred". I'm using eclipse Juno on Windows XP. I tried changing &mytext[0] with mytext in the recv(), but I have the same situation. Which can be the error?

Embedded linux usb-ftdi serial port read issue

I have a TI Cortex-based Pengwyn board running embedded Linux that I am trying to use to read raw serial data from a USB-ftdi peripheral so I can process it into data packets.
To do this I've written a simple program (using Qt) and the termios framework. This works no problem on my desktop (Ubuntu VM) machine - it opens /dev/ttyUSB0, configures the serial port, reads and process the data without problems.
I run into problems when I run the same program (cross-compiled for arm obviously) on the pengwyn board... The read() function call does not populate the buffer - although it returns a non-error value, indicating the number of bytes that have been read???
(I also had to rebuilt the linux kernel for the pengwyn board to include teh USB-ftdi serial driver.)
I suspect it is a target configuration issue but I have compared the file permissions and termios settings between both the platforms and they look okay. I am new to this platform and serial/termios stuff so I have undoubtedly over-looked something but I've looked through the "Serial Programming Guide for POSIX Operating Systems" and searched for similar posts regarding arm and usb-ftdi read problems but have yet to find anything.
Any suggestions/observations?
Relevant excerpts from test program:
void Test::startRx(void)
{
bool retval = false;
m_fd = open(m_port.toLocal8Bit(),O_RDONLY |O_NOCTTY | O_NDELAY);
if (m_fd == -1)
{
qDebug() << "Unable to open: " << m_port.toLocal8Bit() << strerror(errno);
}
else
{
m_isConnected = true;
qDebug() << m_port.toLocal8Bit() << "is open...";
fcntl(m_fd, F_SETFL, 0);
struct termios options;
if (tcgetattr(m_fd, &options)!=0)
{
qDebug() << "tcgetattr() failed";
}
//Set the baud rates to 9600
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
//Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
//Set character size
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
//No parity 8N1:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//Disable hardware flow control
options.c_cflag &= ~CRTSCTS;
//Disable software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
//Raw output
options.c_oflag &= ~OPOST;
//Raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//Set the new options for the port...
tcsetattr(m_fd, TCSANOW, &options);
int status;
ioctl(m_fd, TIOCMGET, &status );
qDebug() << "current modem status:" << status;
QThread* m_reader_thread = new QThread;
m_serial_port_reader = new SerialPortReader(0, m_fd);
if (m_serial_port_reader)
{
qDebug() << "creating serial port reader thread...";
m_serial_port_reader->moveToThread(m_reader_thread);
connect(m_serial_port_reader, SIGNAL(notifyRxPacketData(QByteArray *)), this, SLOT(processRxPacketData(QByteArray*)));
//connect(m_serial_port_reader, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(m_reader_thread, SIGNAL(started()), m_serial_port_reader, SLOT(process()));
connect(m_serial_port_reader, SIGNAL(finished()), m_reader_thread, SLOT(quit()));
connect(m_serial_port_reader, SIGNAL(finished()), m_serial_port_reader, SLOT(quit()));
connect(m_reader_thread, SIGNAL(finished()), m_reader_thread, SLOT(deleteLater()));
m_reader_thread->start();
retval = true;
}
....
void SerialPortReader::readData(int i)
{
m_socket_notifier->setEnabled(false);
if (i == m_fd)
{
unsigned char buf[BUFSIZE] = {0};
int bytesRead = read(m_fd, buf, BUFSIZE);
//qDebug() << Q_FUNC_INFO << "file decriptor:" << m_fd << ", no. bytes read:" << bytesRead;
if (bytesRead < 0)
{
qDebug() << Q_FUNC_INFO << "serial port read error";
return;
}
// if the device "disappeared", e.g. from USB, we get a read event for 0 bytes
else if (bytesRead == 0)
{
//qDebug() << Q_FUNC_INFO << "finishing!!!";
return;
}
//process data...
}
m_socket_notifier->setEnabled(true);
}

Resources