visual studio access violation reading location 0xc0000005 - c++

I am receiving data in TCP in C++ using Qt library. I store the received packet in a QByteArray, but after reading the whole data, I face this error in debug. At the end, I try to clear the buffer, but I face this problem while trying to clear it too.
Here is my code :
void AvaNPortTester::scoket_readyRead()
{
ui.lineEdit_Sending_Status_->setText("Sent");
ui.lineEdit_Sending_Status_->setStyleSheet("QLineEdit { background: rgb(50, 255, 50); }");
tcpSocket_data_buffer_.append(tcpSocket_->readAll());
//qDebug() << serialport_data_buffer_.size();
//auto ddd = QString::number(tcpSocket_data_buffer_.size());// +" : " + tcpSocket_data_buffer_.toHex();
//ui.lableSocketRead->setText(ddd);
bool read_aain = false;
QByteArray dummy(int(1446), Qt::Initialization::Uninitialized);
int reminded_data = 0;
int dummy_size = 0;
int frame_size = 0;
int l_size = 0;
int total_size_rcvd = tcpSocket_data_buffer_.size();
//int total_size_rcvd_b = total_size_rcvd_b;
int temp = 0;
while (total_size_rcvd != 0)
{
if(total_size_rcvd != 0){
auto packet = tcpSocket_data_buffer_.mid(0, 1446);
auto rem = tcpSocket_data_buffer_.mid(1446);//****1146
tcpSocket_data_buffer_ = rem;
QDataStream streamdata(packet);
uint8_t Sync_Header[3];
auto ss = streamdata.readRawData((char*)&Sync_Header, 3);
uint8_t Total_size[2];
ss = streamdata.readRawData((char*)&Total_size, 2);
int t_size = Total_size[0] * 256 + Total_size[1];
uint8_t Reserved[2];
ss = streamdata.readRawData((char*)&Reserved, 2);
frame_size = t_size - 2;
reminded_data = t_size - 2;
while (frame_size != 0)
{
uint8_t portid;
ss = streamdata.readRawData((char*)&portid, 1);
//ui.lineEdit_FileSize->setText(QString::number(fileSend_2Ser->size()));
uint8_t ProtocolID;
ss = streamdata.readRawData((char*)&ProtocolID, 1);
uint8_t MoreFragmentFlag;
ss = streamdata.readRawData((char*)&MoreFragmentFlag, 1);
uint8_t Seq;
ss = streamdata.readRawData((char*)&Seq, 1);
uint8_t size[2];
ss = streamdata.readRawData((char*)&size, 2);
l_size = size[0] * 256 + size[1];
if (packet_flags.Ser2Eth.packet_started[portid] == false) {
uint8_t DDCMP_Header[14];
ss = streamdata.readRawData((char*)&DDCMP_Header, 14);
packet_flags.Ser2Eth.protocol_payload_size[portid] = DDCMP_Header[7] + 256 * DDCMP_Header[8];
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
packet_flags.Ser2Eth.packet_started[portid] = true;
}
QByteArray ddcmp_datap(int(l_size), Qt::Initialization::Uninitialized);
streamdata.readRawData(ddcmp_datap.data(), l_size - 14);
if ((pre_more_frag == 0) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 0) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size + 16;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
if (MoreFragmentFlag == 1) {
pre_more_frag = 1;
}
else {
pre_more_frag = 0;
}
int ff = 0;
if (packet_flags.Ser2Eth.packet_ended[portid] == true) {
packet_flags.Ser2Eth.packet_started[portid] = false;
packet_flags.Ser2Eth.packet_started[portid] = false;
set_port_id_flag(portid, packet_flags.Ser2Eth.protocol_payload_size[portid], ProtocolID);
pre_more_frag = 0;
}
reminded_data = reminded_data - 6 - l_size;
//ui.lableSocketRead->setText(ddcmp_datap.toHex());
frame_size = frame_size - l_size - 6;
}//end of while (frame_size != 0)
uint8_t sync_footer[3];
streamdata.readRawData((char *)&sync_footer, 3);
dummy_size = 1446 - t_size - 8;
uint8_t dummy_data[1000];
streamdata.readRawData((char *)&dummy_data, dummy_size);
total_size_rcvd = total_size_rcvd - 1446;
if (total_size_rcvd == 0) {
tcpSocket_data_buffer_.clear();
}
} //end of if
}//end of while()
}

Related

How to manipulate audio data buffers correctly?

I have implemented recording and playing back audio from a microphone in C++. The next step is to process the audio data for speech recognition. For this I want to write them to large buffers so that there are no word breaks. To do this, I implemented copying to large buffers using the memcpy function. Unfortunately, it doesn't work because only part of words can be recognized. What is my mistake and can this buffer manipulation be done in a more convenient way?
My code:
#include <stdio.h>
#include <Windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
using namespace std;
#pragma comment(lib, "winmm.lib")
#define Samples 16000
#define NUM_FRAMES Samples*2
#define Channels 1
const int NUM_BUF = 4;
int main()
{
HWAVEIN inStream;
HWAVEOUT outStream;
WAVEFORMATEX waveFormat;
WAVEHDR buffer[NUM_BUF];
waveFormat.cbSize = 0;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nChannels = Channels;
waveFormat.nSamplesPerSec = Samples;
waveFormat.wBitsPerSample = 16;
waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
waveFormat.nAvgBytesPerSec = waveFormat.nBlockAlign * waveFormat.nSamplesPerSec;
HANDLE event = CreateEventA(NULL, TRUE, FALSE, "waveout event");
MMRESULT res = MMSYSERR_NOERROR;
res = waveInOpen(&inStream, WAVE_MAPPER, &waveFormat, (unsigned long)event, 0, CALLBACK_EVENT);
if (res != MMSYSERR_NOERROR) {
printf("error in waveInOpen\n");
return -1;
}
res = waveOutOpen(&outStream, WAVE_MAPPER, &waveFormat, (unsigned long)event, 0, CALLBACK_EVENT);
if (res != MMSYSERR_NOERROR) {
printf("error in waveOutOpen\n");
return -2;
}
short int *_pBuf;
size_t bpbuff = 16000*2;
_pBuf = new short int [bpbuff * NUM_BUF];
for ( int i = 0; i < NUM_BUF; i++ )
{
buffer[i].lpData = (LPSTR)&_pBuf [i * bpbuff];
buffer[i].dwBufferLength = bpbuff*sizeof(*_pBuf);
buffer[i].dwFlags = 0L;
buffer[i].dwLoops = 0L;
waveInPrepareHeader(inStream, & buffer[i], sizeof(WAVEHDR));
}
ResetEvent(event);
for (int index = 0; index < NUM_BUF; index++) // queue all buffers for input
waveInAddBuffer(inStream, &buffer[index], sizeof(WAVEHDR));
waveInStart(inStream);
int len_buff = buffer[0].dwBufferLength*6 + 1;
int limit_buff = buffer[0].dwBufferLength*5 + 1;
int size = buffer[0].dwBufferLength;
int rl = 0;
int flagg = 0;
char * buff1 = new char[len_buff];
char * buff2 = new char[len_buff];
int flag_buf = 0;
int flag1 = 0, flag2 = 0;
int i = 0;
int inIndex = 0, outIndex = 0; // the next input and output to watch
while (true) {
if (buffer[inIndex].dwFlags & WHDR_DONE & flagg!=1)
{
flagg = 1;
waveInAddBuffer(inStream, &buffer[inIndex], sizeof(WAVEHDR));
inIndex = (inIndex + 1) % NUM_BUF;
}
if (buffer[outIndex].dwFlags & WHDR_DONE & flagg!=0) {
flagg = 0;
if (flag_buf == 0)
{
if (rl<limit_buff)
{
cout << rl << endl;
if (flag1 == 0)
{
//strcpy(buff1, buffer[outIndex].lpData);
memcpy(buff1, buffer[outIndex].lpData, size);
flag1 = 1;
rl = size + 1;
}
else
{
//strcat(buff1, buffer[outIndex].lpData);
memcpy(buff1 + rl, buffer[outIndex].lpData, size);
rl = rl + size;
}
}
else
{
//recognize buff1
flag_buf = 1;
flag1 = 0;
rl = 0;
}
}
else
{
if (rl<limit_buff)
{
if (flag2 == 0)
{
memcpy(buff2, buffer[outIndex].lpData, size);
flag2 = 1;
rl = size + 1;
}
else
{
memcpy(buff2 + rl, buffer[outIndex].lpData, size);
rl = rl + size;
}
}
else
{
//recognize buff2
flag_buf = 0;
flag2 = 0;
rl = 0;
}
}
waveOutWrite(outStream, &buffer[outIndex], sizeof(WAVEHDR));
outIndex = (outIndex + 1) % NUM_BUF;
printf("N_buff_%i %i\n",outIndex , i);
i++;
}
}
for (int index = 0; index < 4; index++)
waveInUnprepareHeader(inStream, &buffer[inIndex], sizeof(WAVEHDR));
free(buffer);
}

Eigen: static assertion failed

I need to use the eigen library in my project on Ubuntu (this project works fine on windows). However when I compile I receive the following error:
dv_qkd_ldpc_tx_error_correction_20200819.cpp:186:91: required from here
Eigen/src/Core/util/XprHelper.h:835:96: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
Acording to terminal, this error is caused by one of my .cpp file at the line 186:
*pt_calculatedSindrome = sparseParityCheckMatrix * (*pt_receivedData);
or
*pt_calculatedSindrome = (*pt_calculatedSindrome).unaryExpr([](const t_binary x) {return x % 2;}); //to compute mod(sindrome,2)
I understand that this error is asking to apply some casting on the matrixes. And according to this page, I applied the following which didn't work:
*pt_calculatedSindrome = (sparseParityCheckMatrix.cast<t_binary>()) * ((*pt_receivedData).cast<t_binary>());
*pt_calculatedSindrome = ((*pt_calculatedSindrome).cast<t_binary>()).unaryExpr([](const t_binary x) {return x % 2;}); //to compute mod(sindrome,2)
Edit: in which:
t_binary = uint8_t
Even when I comment these lines (or even when I remove the whole lines of the file), I still get the same error with the same error line!!! I checked my Makefile to make sure it builds all the .cpp files and it seems to be ok.
Here are the terminal errors:
In file included from Eigen/Core:369,
from Eigen/SparseCore:11,
from Eigen/Sparse:26,
from netxpto_20200819.h:48,
from dv_qkd_ldpc_tx_error_correction_20200819.h:4,
from dv_qkd_ldpc_tx_error_correction_20200819.cpp:1:
Eigen/src/Core/AssignEvaluator.h: In instantiation of ‘void Eigen::internal::call_assignment_no_alias(Dst&, const Src&, const Func&) [with Dst = Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1>; Src = Eigen::CwiseUnaryOp<DvQkdLdpcTxErrorCorrection::runBlock()::<lambda(t_binary)>, const Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1> >; Func = Eigen::internal::assign_op<unsigned char, int>]’:
Eigen/src/Core/AssignEvaluator.h:804:27: required from ‘void Eigen::internal::call_assignment(Dst&, const Src&, const Func&, typename Eigen::internal::enable_if<(! Eigen::internal::evaluator_assume_aliasing<Src>::value), void*>::type) [with Dst = Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1>; Src = Eigen::CwiseUnaryOp<DvQkdLdpcTxErrorCorrection::runBlock()::<lambda(t_binary)>, const Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1> >; Func = Eigen::internal::assign_op<unsigned char, int>; typename Eigen::internal::enable_if<(! Eigen::internal::evaluator_assume_aliasing<Src>::value), void*>::type = void*]’
Eigen/src/Core/AssignEvaluator.h:782:18: required from ‘void Eigen::internal::call_assignment(Dst&, const Src&) [with Dst = Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1>; Src = Eigen::CwiseUnaryOp<DvQkdLdpcTxErrorCorrection::runBlock()::<lambda(t_binary)>, const Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1> >]’
Eigen/src/Core/PlainObjectBase.h:714:32: required from ‘Derived& Eigen::PlainObjectBase<Derived>::_set(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<DvQkdLdpcTxErrorCorrection::runBlock()::<lambda(t_binary)>, const Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1> >; Derived = Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1>]’
Eigen/src/Core/Matrix.h:225:24: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::operator=(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<DvQkdLdpcTxErrorCorrection::runBlock()::<lambda(t_binary)>, const Eigen::Matrix<unsigned char, -1, 1, 0, -1, 1> >; _Scalar = unsigned char; int _Rows = -1; int _Cols = 1; int _Options = 0; int _MaxRows = -1; int _MaxCols = 1]’
dv_qkd_ldpc_tx_error_correction_20200819.cpp:186:89: required from here
Eigen/src/Core/util/XprHelper.h:835:96: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
835 | ernal::has_ReturnType<ScalarBinaryOpTraits<LHS, RHS,BINOP> >::value), \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
Eigen/src/Core/util/StaticAssert.h:33:54: note: in definition of macro ‘EIGEN_STATIC_ASSERT’
33 | #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
| ^
Eigen/src/Core/AssignEvaluator.h:834:3: note: in expansion of macro ‘EIGEN_CHECK_BINARY_COMPATIBILIY’
834 | EIGEN_CHECK_BINARY_COMPATIBILIY(Func,typename ActualDstTypeCleaned::Scalar,typename Src::Scalar);
Makefile:91: recipe for target 'dv_qkd_ldpc_tx_error_correction_20200819.o' failed
Here is .cpp file causing error:
#include "cv_qokd_ldpc_tx_sindrome_reconciliation_20200819.h"
void CvQokdLdpcTxSindromeReconciliation::initialize(void)
{
if (errorCorrectionMethod == CvQokdLdpcTxSindromeReconciliation::t_method::LDPC)
{
loadSparseParityCheckMatrix();
sparseParityCheckMatrixTranspose = sparseParityCheckMatrix.transpose();
calculatedSindromeSize_0 = codeLength - codeRank;
calculatedSindrome_0.resize(calculatedSindromeSize_0, 1);
calculatedSindrome_0_posIn = 0;
calculatedSindrome_0_posOut = calculatedSindrome_0.size();
calculatedSindrome_0_newData = false;
calculatedSindromeSize_1 = codeLength - codeRank;
calculatedSindrome_1.resize(calculatedSindromeSize_1, 1);
calculatedSindrome_1_posIn = 0;
calculatedSindrome_1_posOut = calculatedSindrome_1.size();
calculatedSindrome_1_newData = false;
receivedSindromeSize = 1; // codeLength - codeRank;
receivedSindrome.resize(receivedSindromeSize, 1);
receivedSindrome_posIn = 0;
receivedSindrome_posOut = receivedSindrome.size();
receivedSindrome_newData = false;
receivedDataSize_0 = codeLength;
receivedData_0.resize(receivedDataSize_0, 1);
receivedData_0_posIn = 0;
receivedData_0_posOut = receivedData_0.size();
receivedData_0_newData = false;
receivedDataSize_1 = codeLength;
receivedData_1.resize(receivedDataSize_1, 1);
receivedData_1_posIn = 0;
receivedData_1_posOut = receivedData_1.size();
receivedData_1_newData = false;
DataIn.resize((size_t)4*inputSignals[0]->getBufferLength());
DataOut.resize(DataIn.size());
SindromeIn.resize((size_t)4*inputSignals[1]->getBufferLength());
SindromeOut.resize(SindromeIn.size());
prioriMessageProbabilities.resize(codeLength, 1);
M.resize(codeLength - codeRank, codeLength);
M.reserve(numberOfNonZerosValuesInSparseParityCheckMatrix);
E.resize(codeLength - codeRank, codeLength);
E.reserve(numberOfNonZerosValuesInSparseParityCheckMatrix);
tripletList.reserve(getNumberOfNonZerosValuesInSparseParityCheckMatrix());
bitFillingPeriod = numberOfDataBitsPerBitFillingPeriod + numberOfDummyBitsPerBitFillingPeriod;
}
else
{
std::cerr << "DVQkdErrorCorrectionLDPC::initialize() - wrong error correction method";
}
startSendingSindrome = (getErrorCorrectionRole() == t_role::Rx && getErrorCorrectionMode() == t_mode::InverseReconciliation) || (getErrorCorrectionRole() == t_role::Tx && getErrorCorrectionMode() == t_mode::DirectReconciliation);
}
bool CvQokdLdpcTxSindromeReconciliation::runBlock(void)
{
if (bypassErrorCorrection)
{
auto ready = inputSignals[0]->ready();
auto space = outputSignals[0]->space();
auto process = std::min(ready, space);
totalNumberOfInputBits += process;
totalNumberOfOutputBits += process;
for (; process--;)
{
t_binary dataIn{ 0 };
inputSignals[0]->bufferGet(&dataIn);
outputSignals[0]->bufferPut(dataIn);
}
for (auto k = 1; k < inputSignals.size(); k++)
{
ready = inputSignals[k]->ready();
space = inputSignals[k]->space();
process = std::min(ready, space);
for (; process--;)
inputSignals[k]->bufferGet();
}
return false;
}
bool alive{ false };
Eigen::Index ready = DataOut.ready();
Eigen::Index space = outputSignals[0]->space();
Eigen::Index process = std::min(ready, space);
alive = !alive ? process > 0: alive;
totalNumberOfOutputBits += process;
for (; process--;)
outputSignals[0]->bufferPut(DataOut.bufferGet());
ready = SindromeOut.ready();
space = outputSignals[1]->space();
process = std::min(ready, space);
alive = !alive ? process > 0: alive;
for (; process--;)
outputSignals[1]->bufferPut(SindromeOut.bufferGet());
ready = inputSignals[0]->ready();
space = DataIn.space();
process = std::min(ready, space);
alive = !alive ? process > 0: alive;
totalNumberOfInputBits += process;
for (; process--;)
{
t_binary val{ 0 };
inputSignals[0]->bufferGet(&val);
DataIn.bufferPut(val);
}
ready = inputSignals[1]->ready();
space = SindromeIn.space();
process = std::min(ready, space);
alive = !alive ? process > 0: alive;
for (; process--;)
{
t_binary val{ 0 };
inputSignals[1]->bufferGet(&val);
SindromeIn.bufferPut(val);
}
ready = SindromeIn.ready();
process = std::min(ready, receivedSindromeSize - receivedSindrome_posIn);
alive = !alive ? process > 0: alive;
for (auto k = receivedSindrome_posIn; k < receivedSindrome_posIn + process; k++)
receivedSindrome(k, 0) = SindromeIn.bufferGet();
receivedSindrome_posIn += process;
if (process > 0 && receivedSindrome_posIn == receivedSindromeSize)
{
if (getVerboseMode())
{
std::ofstream file;
file.open(getSignalsFolderName() + "/received_sindrome_tx.txt", std::ios_base::app);
for (auto k = 0; k < receivedSindromeSize; k++)
{
if (k != 0) file << " ";
file << std::to_string(receivedSindrome[k]);
}
file << std::endl;
file.close();
}
receivedSindrome_newData = true;
}
ready = DataIn.ready();
process = std::min( ready, *pt_receivedDataSize - *pt_receivedData_posIn);
alive = !alive ? process > 0: alive;
for (auto k = *pt_receivedData_posIn; k < *pt_receivedData_posIn + process; k++)
{
if (numberOfDummyBitsPerBitFillingPeriod)
{
if (*pt_inDataBitFillingCounter >= numberOfDataBitsPerBitFillingPeriod)
{
(*pt_receivedData)[k] = 0;
}
else
(*pt_receivedData)[k] = DataIn.bufferGet();
*pt_inDataBitFillingCounter = ++(*pt_inDataBitFillingCounter) % bitFillingPeriod;
}
else
(*pt_receivedData)[k] = DataIn.bufferGet();
}
*pt_receivedData_posIn += process;
if (process > 0 && *pt_receivedData_posIn == *pt_receivedDataSize)
*pt_receivedData_newData = true;
if (*pt_receivedData_newData && *pt_calculatedSindrome_posIn == 0 && *pt_calculatedSindrome_newData == false)
{
if (getVerboseMode())
{
std::ofstream file;
file.open(getSignalsFolderName() + "/receivedData_tx.txt", std::ios_base::app);
for (auto k = 0; k < *pt_receivedDataSize; k++)
{
if (k != 0 && k % 200 == 0) file << std::endl;
file << std::to_string((*pt_receivedData)[k]);
}
file << std::endl;
file.close();
}
*pt_calculatedSindrome = sparseParityCheckMatrix * *pt_receivedData;
*pt_calculatedSindrome = (*pt_calculatedSindrome).unaryExpr([](const t_binary x) {return x % 2;}); //to compute mod(sindrome,2)
numberOfCalculatedSindromes++;
*pt_receivedData_posIn = *pt_receivedDataSize;
*pt_receivedData_posOut = *pt_receivedDataSize;
*pt_receivedData_newData = false;
if (getVerboseMode())
{
std::ofstream file;
file.open(getSignalsFolderName() + "/calculated_sindrome_tx.txt", std::ios_base::app);
for (auto k = 0; k < *pt_calculatedSindromeSize; k++)
{
if (k != 0) file << " ";
file << std::to_string((*pt_calculatedSindrome)[k]);
}
file << std::endl;
file.close();
}
*pt_calculatedSindrome_posIn = *pt_calculatedSindromeSize;
*pt_calculatedSindrome_newData = true;
if (process_block == 1)
{
if (startSendingSindrome)
{
calculatedSindrome_1_posOut = 0;
startedSendingSindrome_1 = true;
}
process_block = 0;
pt_receivedDataSize = &receivedDataSize_0;
pt_receivedData = &receivedData_0;
pt_receivedData_posIn = &receivedData_0_posIn;
pt_receivedData_posOut = &receivedData_0_posOut;
pt_receivedData_newData = &receivedData_0_newData;
pt_calculatedSindrome = &calculatedSindrome_0;
pt_calculatedSindromeSize = &calculatedSindromeSize_0;
pt_calculatedSindrome_posIn = &calculatedSindrome_0_posIn;
pt_calculatedSindrome_posOut = &calculatedSindrome_0_posOut;
pt_calculatedSindrome_newData = &calculatedSindrome_0_newData;
pt_inDataBitFillingCounter = &inDataBitFillingCounter_0;
pt_outDataBitFillingCounter = &outDataBitFillingCounter_0;
}
else
{
if (startSendingSindrome)
{
calculatedSindrome_0_posOut = 0;
startedSendingSindrome_0 = true;
}
process_block = 1;
pt_receivedDataSize = &receivedDataSize_1;
pt_receivedData = &receivedData_1;
pt_receivedData_posIn = &receivedData_1_posIn;
pt_receivedData_posOut = &receivedData_1_posOut;
pt_receivedData_newData = &receivedData_1_newData;
pt_calculatedSindrome = &calculatedSindrome_1;
pt_calculatedSindromeSize = &calculatedSindromeSize_1;
pt_calculatedSindrome_posIn = &calculatedSindrome_1_posIn;
pt_calculatedSindrome_posOut = &calculatedSindrome_1_posOut;
pt_calculatedSindrome_newData = &calculatedSindrome_1_newData;
pt_inDataBitFillingCounter = &inDataBitFillingCounter_1;
pt_outDataBitFillingCounter = &outDataBitFillingCounter_1;
}
}
ready = calculatedSindromeSize_0 - calculatedSindrome_0_posOut;
space = SindromeOut.space();
process = std::min(ready, space);
if ( startedSendingSindrome_0 && (!startedSendingSindrome_1) && process)
{
for (auto k = calculatedSindrome_0_posOut; k < calculatedSindrome_0_posOut + process; k++)
SindromeOut.bufferPut(calculatedSindrome_0[k]);
calculatedSindrome_0_posOut = calculatedSindrome_0_posOut + process;
if (calculatedSindrome_0_posIn == calculatedSindromeSize_0) calculatedSindrome_0_posIn = 0;
if (calculatedSindrome_0_posOut == calculatedSindromeSize_0)
startedSendingSindrome_0 = false;
}
ready = calculatedSindromeSize_1 - calculatedSindrome_1_posOut;
space = SindromeOut.space();
process = std::min(ready, space);
if (startedSendingSindrome_1 && (!startedSendingSindrome_0) && process)
{
for (auto k = calculatedSindrome_1_posOut; k < calculatedSindrome_1_posOut + process; k++)
SindromeOut.bufferPut(calculatedSindrome_1[k]);
calculatedSindrome_1_posOut = calculatedSindrome_1_posOut + process;
if (calculatedSindrome_1_posIn == calculatedSindromeSize_1) calculatedSindrome_1_posIn = 0;
if (calculatedSindrome_1_posOut == calculatedSindromeSize_1)
startedSendingSindrome_1 = false;
}
if (receivedSindrome_newData)
{
numberOfValidatedSindromes++;
if (receivedSindrome[0]==0)
{
numberOfNoMatchedSindromes++;
receivedData_0_posIn = 0;
receivedData_1_posIn = 0;
}
else
{
receivedData_0_posOut = 0;
receivedData_1_posOut = 0;
}
receivedSindrome_posIn = 0;
receivedSindrome_newData = false;
calculatedSindrome_0_newData = false;
calculatedSindrome_1_newData = false;
calculatedSindrome_0_posOut = 0;
calculatedSindrome_1_posOut = 0;
}
ready = std::min(receivedDataSize_0 - receivedData_0_posOut, receivedDataSize_1 - receivedData_1_posOut);
space = (Eigen::Index) (DataOut.space()/3);
process = std::min(ready, space);
if (process)
{
for (auto k = 0; k < process; k++)
{
if (numberOfDummyBitsPerBitFillingPeriod)
{
if (*pt_outDataBitFillingCounter < numberOfDataBitsPerBitFillingPeriod)
{
DataOut.bufferPut(receivedData_0[receivedData_0_posOut + k]);
DataOut.bufferPut(receivedData_1[receivedData_1_posOut + k]);
}
*pt_outDataBitFillingCounter = ++(*pt_outDataBitFillingCounter) % bitFillingPeriod;
}
else
{
DataOut.bufferPut((t_binary) 0);
DataOut.bufferPut(receivedData_0[receivedData_0_posOut + k]);
DataOut.bufferPut(receivedData_1[receivedData_1_posOut + k]);
}
}
receivedData_0_posOut = receivedData_0_posOut + process;
receivedData_1_posOut = receivedData_1_posOut + process;
if (receivedData_0_posIn == receivedDataSize_0) receivedData_0_posIn = 0;
if (receivedData_1_posIn == receivedDataSize_1) receivedData_1_posIn = 0;
if (sindromeMatch) sindromeMatch = false;
}
ready = DataOut.ready();
space = outputSignals[0]->space();
process = std::min(ready, space);
alive = !alive ? process > 0: alive;
totalNumberOfOutputBits += process;
for (; process--;)
outputSignals[0]->bufferPut(DataOut.bufferGet());
ready = SindromeOut.ready();
space = outputSignals[1]->space();
process = std::min(ready, space);
alive = !alive ? process > 0: alive;
for (; process--;)
outputSignals[1]->bufferPut(SindromeOut.bufferGet());
ready = SindromeIn.ready();
process = std::min(ready, receivedSindromeSize - receivedSindrome_posIn);
alive = !alive ? process > 0: alive;
for (auto k = receivedSindrome_posIn; k < receivedSindrome_posIn + process; k++)
receivedSindrome(k, 0) = SindromeIn.bufferGet();
receivedSindrome_posIn += process;
if (process > 0 && receivedSindrome_posIn == receivedSindromeSize)
{
if (getVerboseMode())
{
std::ofstream file;
file.open("/received_sindrome_tx.txt", std::ios_base::app);
for (auto k = 0; k < receivedSindromeSize; k++)
file << std::to_string(receivedSindrome[k]);
file.close();
}
receivedSindrome_newData = true;
}
ready = DataIn.ready();
process = std::min(ready, (*pt_receivedDataSize) - (*pt_receivedData_posIn));
alive = !alive ? process > 0: alive;
for (auto k = *pt_receivedData_posIn; k < *pt_receivedData_posIn + process; k++)
{
if (numberOfDummyBitsPerBitFillingPeriod)
{
if (*pt_inDataBitFillingCounter >= numberOfDataBitsPerBitFillingPeriod)
{
(*pt_receivedData)[k] = 0;
}
else
(*pt_receivedData)[k] = DataIn.bufferGet();
*pt_inDataBitFillingCounter = ++(*pt_inDataBitFillingCounter) % bitFillingPeriod;
}
else
(*pt_receivedData)[k] = DataIn.bufferGet();
}
*pt_receivedData_posIn += process;
if (process > 0 && *pt_receivedData_posIn == *pt_receivedDataSize)
*pt_receivedData_newData = true;
return alive;
}
t_bool CvQokdLdpcTxSindromeReconciliation::errorCorrectionLDPCAlgorithm(Eigen::Matrix<t_binary, Eigen::Dynamic, 1>* pt_calculatedSindrome, Eigen::Matrix<t_binary, Eigen::Dynamic, 1>* pt_receivedData)
{
for (int k = 0; k < codeLength; ++k)
{
if ((*pt_receivedData)[k] == 0)
prioriMessageProbabilities[k] = log((1 - p_f) / p_f);
else
prioriMessageProbabilities[k] = log(p_f / (1 - p_f));
}
for (int k = 0; k < sparseParityCheckMatrix.outerSize(); ++k) // iterates over the columns
{
for (Eigen::SparseMatrix<t_binary>::InnerIterator h(sparseParityCheckMatrix, k); h; ++h) //iterates over the non-zero elements of each column
{
tripletList.push_back(T((int)h.row(), k, prioriMessageProbabilities[k]));
}
}
M.setFromTriplets(tripletList.begin(), tripletList.end());
tripletList.resize(0);
auto sindromeNoMatch{ 0 };
for (auto m = 0; m < ldpcErrorCorrectionAlgorithMaxIterations; ++m)
{
for (int k = 0; k < sparseParityCheckMatrixTranspose.outerSize(); ++k) // iterates over the columns of H^T, i.e. the lines of H
{
for (Eigen::SparseMatrix<t_binary>::InnerIterator h(sparseParityCheckMatrixTranspose, k); h; ++h) //iterates over the non-zero elements of each column of H^T, i.e. over the non-zero elements of each line of H
{
t_real prod{ 1 };
for (Eigen::SparseMatrix<t_binary>::InnerIterator o(sparseParityCheckMatrixTranspose, k); o; ++o) //iterates over the non-zero elements of each column of H^T, i.e. over the non-zero elements of each line of H
{
if (o.row() != h.row())
prod = prod * tanh(M.coeff(k, (int)o.row()) / 2);
}
tripletList.push_back(T(k, (int)h.row(), log(1 + (1 - 2 * (t_real)receivedSindrome[k]) * prod) - log(1 - (1 - 2 * (t_real)receivedSindrome[k]) * prod)));
}
}
E.setFromTriplets(tripletList.begin(), tripletList.end());
tripletList.resize(0);
for (int k = 0; k < codeLength; ++k)
{
t_real sum{ 0 };
for (Eigen::SparseMatrix<t_binary>::InnerIterator h(sparseParityCheckMatrix, k); h; ++h)
{
sum = sum + E.coeff((int)h.row(), k);
}
auto L = sum + prioriMessageProbabilities[k];
if (L <= 0.0)
(*pt_receivedData)[k] = 1;
else
(*pt_receivedData)[k] = 0;
}
*pt_calculatedSindrome = sparseParityCheckMatrix * *pt_receivedData;
*pt_calculatedSindrome = (*pt_calculatedSindrome).unaryExpr([](const t_binary x) {return x % 2;}); //to compute mod(sindrome,2)
sindromeNoMatch = 0;
for (auto k = 0; k < *pt_calculatedSindromeSize; k++)
if ((*pt_calculatedSindrome)[k] != receivedSindrome[k])
sindromeNoMatch++;
if (sindromeNoMatch == 0)
{
if (getVerboseMode())
{
std::ofstream file;
file.open( getSignalsFolderName() + "/new_sindrome_tx.txt", std::ios_base::app);
if (file.is_open())
{
for (auto k = 0; k < *pt_calculatedSindromeSize; k++)
file << std::to_string((*pt_calculatedSindrome)[k]);
file << std::endl;
file.close();
}
else
std::cerr << "DV_QKD_LDPC_TX_Error_Correction: Error opening file";
}
break;
}
for (int k = 0; k < sparseParityCheckMatrix.outerSize(); ++k) // iterates over the columns of H
{
for (Eigen::SparseMatrix<t_binary>::InnerIterator h(sparseParityCheckMatrix, k); h; ++h) //iterates over the non-zero elements of each column of H
{
t_real sum{ 0 };
for (Eigen::SparseMatrix<t_binary>::InnerIterator o(sparseParityCheckMatrix, k); o; ++o) //iterates over the non-zero elements of each column of H^T, i.e. over the non-zero elements of each line of H
{
if (o.row() != h.row())
sum = sum + E.coeff((int)o.row(), k);
}
tripletList.push_back(T((int)h.row(), k, sum + prioriMessageProbabilities[k]));
}
}
M.setFromTriplets(tripletList.begin(), tripletList.end());
tripletList.resize(0);
}
if (sindromeNoMatch > 0)
return false;
else
return true;
}
void CvQokdLdpcTxSindromeReconciliation::loadSparseParityCheckMatrix()
{
auto pos_a = parityCheckMatrixFileName.find("_n");
auto pos_b = parityCheckMatrixFileName.find("_k");
codeLength = stoi(parityCheckMatrixFileName.substr(pos_a + 2, pos_b - pos_a - 2));
pos_a = parityCheckMatrixFileName.find("_k");
pos_b = parityCheckMatrixFileName.find("_nonZeros");
codeRank = stoi(parityCheckMatrixFileName.substr(pos_a + 2, pos_b - pos_a - 2));
pos_a = parityCheckMatrixFileName.find("_nonZeros");
pos_b = parityCheckMatrixFileName.find(".dat");
numberOfNonZerosValuesInSparseParityCheckMatrix = stoi(parityCheckMatrixFileName.substr(pos_a + 9, pos_b - pos_a - 9));
sparseParityCheckMatrix.resize(codeLength - codeRank, codeLength);
sparseParityCheckMatrix.reserve(numberOfNonZerosValuesInSparseParityCheckMatrix);
if (parityCheckMatrixFileName.substr(parityCheckMatrixFileName.find_last_of(".") + 1) == "txt")
{
std::fstream fp;
fp.open(parityCheckMatrixFolderName + "/" + parityCheckMatrixFileName, std::ios::in);
if (!fp.is_open()) std::cerr << "Error in dv_qkd_ldpc_error_correction_.cpp: file sparseParityCheckMatrix.txt not found!!!!";
double data;
int row{ 0 }, column{ 0 };
while (true)
{
fp >> data;
if (data != 0)
sparseParityCheckMatrix.insert(row, column) = (uint8_t)data;
column++;
if (column == codeLength)
{
column = 0;
row++;
}
if (row == codeLength - codeRank)
break;
}
fp.close();
}
else if (parityCheckMatrixFileName.substr(parityCheckMatrixFileName.find_last_of(".") + 1) == "dat")
{
std::ifstream parityCheckMatrixFile(parityCheckMatrixFolderName + "/" + parityCheckMatrixFileName);
if (!parityCheckMatrixFile.is_open()) std::cerr << "Error in dv_qkd_ldpc_error_correction_.cpp: file parityCheckMatrixFileName not found!!!!";;
size_t bufferSize = (codeLength - codeRank) * codeLength;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
parityCheckMatrixFile.read(buffer.get(), bufferSize);
int row{ 0 }, column{ 0 };
uint8_t data{ 0 };
char* ptr{ &buffer[0] };
for (auto k = 0; k < bufferSize; ++k)
{
data = *ptr - '0';
if (data != 0)
sparseParityCheckMatrix.insert(row, column) = (uint8_t)data;
ptr++;
column++;
if (column == codeLength)
{
column = 0;
row++;
}
}
}
else
{
std::cerr << "DVQkdRxErrorCorrectionLDPC::loadSparseParityCheckMatrix() - wrong file extension";
}
if (getVerboseMode())
{
std::ofstream file;
file.open(getSignalsFolderName() + "/SparseParityCheckMatrix.txt", std::ios_base::out);
if (file.is_open())
{
file << "Position of no-zero values (row, column)" << std::endl;
for (int k = 0; k < sparseParityCheckMatrix.outerSize(); ++k) // iterates over the columns of H
{
for (Eigen::SparseMatrix<t_binary>::InnerIterator it(sparseParityCheckMatrix, k); it; ++it)
file << "(" << it.row() << ", " << it.col() << ")" << std::endl;
}
file.close();
}
else
std::cerr << "Error in dv_qkd_ldpc_rx_error_correction_.cpp: file not open!!!";
}
return;
}
Any help is appreciated.

C++ base64decode returns junk data if it contains '\0'

We have an certificate file (binary) having '\0' in multiple places. While trying to decode using openssl it gives junk data while size used to be perfect.
Same code works perfectly if there is no '\0' in the base64encoded data
We tried achieving it using below code but still file is not readable
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
int in_1 = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
std::ofstream outfile;
outfile.open("output_file.pfx", std::ios::binary | std::ios::out);
bool f = isalnum(encoded_string[in_]);
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i < 4; i++)
{
char_array_4[i] = base64_chars.find(char_array_4[i]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
{
if (char_array_3[i] != NULL)
{
ret += char_array_3[i];
char val = char_array_3[i];
outfile.write(&val, sizeof(char));
}
else
{
/*char str3[3155];
strcpy(str3, ret.c_str());
ret = "";
ret.append(str3, sizeof(str3));*/
ret += "NUL";
char val111 = char_array_3[i];
outfile.write(&val111, sizeof(char));
}
}
i = 0;
}
}
if (i) {
for (j = 0; j < i; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++)
{
if (char_array_3[i] != NULL)
{
ret += char_array_3[i];
char val1 = char_array_3[i];
outfile.write(&val1, sizeof(char));
}
else
{
ret += "NUL";
char val11 = char_array_3[i];
outfile.write(&val11, sizeof(char));
}
}//ret += char_array_3[j];
}
outfile.close();
return ret;
}
int main()
{
base64_decode("U4tR8mRzyrqpNhhFjkbEe4I6LTYXhL7PxkbddNTQ1yZ6ofZ4s1R/UOQsq6x+CNxB+yddPirwT0yPgtm6IC1qYF9GGQsOqXHkpTrmXf0GiXDVpm91EMnyxtMu74B3OMIYgxmjoeua7HoKQkW6/GRuCpgWIoZQq7uOaKIsc3k9HGgfAFk6vTGER1YJlG28lOhsiGccl0EqD0uhrBGNhFERfAzB2gaJjI1oRO87Q2NbevKHeZycpyXgazvtw9JigA+Hp3+Cy9LUIRvF6k5uv0DKxOs5cynqYslb1LfKqT0IvLjBl4gNHl+pG5/Ur70XzZTiO1+n5jWITPoslZ4slVkl4qiTaqNWHgLT6aSUhWwPlvK+7wlk+st5ykAuSIE2e3Lia+omBRH2LQfG1v7KaOJApF3k4D0li/4QWOJ3zLwBDHB6WCwMQfNS8vTRWM1yIO/o9417wJEpBlcr/B308vGheoTF9+qRKGDe0M5PNHeBbEHhgNkLsKvcS/31HK6Xd36cg85yvyLghQRr9Gyn7TUU5m6f6iSlx3u+yo1vT7BBV6OjbxPklwCIYCZWIIOJq10JXC+bSGPbTKZYXjQW90URKesUOMi9s+DS7BKVEr471AnEyazividrgivfHDNWQisIcOctpDFCfEBAa28PYjIj4KJo5bDkSluRVcVDJVrP2Ns=");
return 0;
}
There is a bug in the handling of the trailing bytes, you are using i as the array index instead of j. As i can be larger than the size of char_array_3 it produces undefined behaviour. The correct code is:
for (j = 0; (j < i - 1); j++)
{
if (char_array_3[j] != NULL)
{
ret += char_array_3[j];
char val1 = char_array_3[j];
outfile.write(&val1, sizeof(char));
}
else
{
ret += "NUL";
char val11 = char_array_3[j];
outfile.write(&val11, sizeof(char));
}
}

How to back propagate a Neural Network in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have been trying to make a neural network in c++ and my back propagation code is not working the way I want it to. I have a text document that tells the network how to function. I have it have 2 input neurons, 1 hidden layer with 4 neurons and 2 output neurons. I have it learning to be an XOR gate right now. I have it so it takes the cost of the network, multiplys it by .55(scaling) and adding/subtracting that from the weights/bias' depending on how close the output is from the correct answer and weither or not the weights/bias' are + or -. Here's the code:
void Network::backProp(void)
{
double b = 0,a;
int loop,l;
for(loop=0;loop<4;loop++)
{
//Adds up the cost of the data
b = b + (pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2));
}
a=.55*b;
if(b>.01)
{
for(l=0;l<4;l++)
{
if(round(results[2*l])!=key[4*l+2])
{
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
if(data[6] <= 0)
{
data[6] = data[6]+a; //(abs(data[6])/a);
}
else
{
data[6] = data[6]-a; //(abs(data[6])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
if(data[14] <= 0)
{
data[14] = data[14]+a; //(abs(data[14])/a);
}
else
{
data[14] = data[14]-a; //(abs(data[14])/a);
}
if(data[16] <= 0)
{
data[16] = data[16]+a; //(abs(data[16])/a);
}
else
{
data[16] = data[16]-a; //(abs(data[16])/a);
}
if(data[18] <= 0)
{
data[18] = data[18]+a; //(abs(data[18])/a);
}
else
{
data[18] = data[18]-a; //(abs(data[18])/a);
}
if(data[20] <= 0)
{
data[20] = data[20]+a; //(abs(data[20])/a);
}
else
{
data[20] = data[20]-a; //(abs(data[20])/a);
}
}
else
{
if(data[0] <= 0)
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
if(data[6] <= 0)
{
data[6] = data[6]-a; //(abs(data[6])/a);
}
else
{
data[6] = data[6]+a; //(abs(data[6])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
if(data[14] <= 0)
{
data[14] = data[14]-a; //(abs(data[14])/a);
}
else
{
data[14] = data[14]+a; //(abs(data[14])/a);
}
if(data[16] <= 0)
{
data[16] = data[16]-a; //(abs(data[16])/a);
}
else
{
data[16] = data[16]+a; //(abs(data[16])/a);
}
if(data[18] <= 0)
{
data[18] = data[18]-a; //(abs(data[18])/a);
}
else
{
data[18] = data[18]+a; //(abs(data[18])/a);
}
if(data[20] <= 0)
{
data[20] = data[20]-a; //(abs(data[20])/a);
}
else
{
data[20] = data[20]+a; //(abs(data[20])/a);
}
}
if(round(results[2*l+1])!=key[4*l+3])
{
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
if(data[5] <= 0)
{
data[5] = data[5]+a; //(abs(data[5])/a);
}
else
{
data[5] = data[5]-a; //(abs(data[5])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
if(data[15] <= 0)
{
data[15] = data[15]+a; //(abs(data[15])/a);
}
else
{
data[15] = data[15]-a; //(abs(data[15])/a);
}
if(data[17] <= 0)
{
data[17] = data[17]+a; //(abs(data[17])/a);
}
else
{
data[17] = data[17]-a; //(abs(data[17])/a);
}
if(data[19] <= 0)
{
data[19] = data[19]+a; //(abs(data[19])/a);
}
else
{
data[19] = data[19]-a; //(abs(data[19])/a);
}
if(data[21] <= 0)
{
data[21] = data[21]+a; //(abs(data[21])/a);
}
else
{
data[21] = data[21]-a; //(abs(data[21])/a);
}
}
else
{
if(data[0] <= 0)
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
if(data[5] <= 0)
{
data[5] = data[5]-a; //(abs(data[5])/a);
}
else
{
data[5] = data[5]+a; //(abs(data[5])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
if(data[15] <= 0)
{
data[15] = data[15]-a; //(abs(data[15])/a);
}
else
{
data[15] = data[15]+a; //(abs(data[15])/a);
}
if(data[17] <= 0)
{
data[17] = data[17]-a; //(abs(data[17])/a);
}
else
{
data[17] = data[17]+a; //(abs(data[17])/a);
}
if(data[19] <= 0)
{
data[19] = data[19]-a; //(abs(data[19])/a);
}
else
{
data[19] = data[19]+a; //(abs(data[19])/a);
}
if(data[21] <= 0)
{
data[21] = data[21]-a; //(abs(data[21])/a);
}
else
{
data[21] = data[21]+a; //(abs(data[21])/a);
}
}
}
}
}
I know it's a mess but this is what I came up with. I can post the rest of the code if that would help.
Here is a simplified version of your code
void Network::backProp(void)
{
double b = 0,a;
int loop,l;
int inclusion1 [] = {0,1,2,3,4,6,7,8,9,10,11,12,13,14,16,18,20};
int inclusion2 [] = {0,1,2,3,4,5,7,8,9,10,11,12,13,15,17,19,21};
int j = 0;
for(loop=0;loop<4;loop++)
{
//Adds up the cost of the data
b = b + (pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2));
}
a=.55*b;
if(b>.01)
{
for(l=0;l<4;l++)
{
for(j=0;j<17;j++)
{
if(round(results[2*l])!=key[4*l+2])
{
data[inclusion1[j]] = data[inclusion1[j]] - abs(data[inclusion1[j]])/data[inclusion1[j]]*a;
}
if(round(results[2*l+1])!=key[4*l+3])
{
data[inclusion2[j]] = data[inclusion2[j]] + abs(data[inclusion2[j]])/data[inclusion2[j]]*a;
}
}
}
}
}
The basic issue as I see is it that your correction variable b i dont think it is defined accurately
Should be more along the lines
b = b + pow((pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2)),1/2);
move this block to seperate function:
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
like this: (find proper name)
void AddAtoData(int& data, a)
{
if(data <= 0)
{
data += a;
}
else
{
data -= a;
}
}
then split your data[] structure to logical units, e.g. your layers to avoid the other if-logic and use loops.
once you cleaned up, look if your problem still exists, if so, come back.

What's wrong with this parallel algorithm?

I'm trying to write parallel algorithm in openCL for L-system Pythagoras Tree :
var:A,B;
const: (,);
axiom:A;
rules:(B->BB),(A->B[A]A)
But i can't get over 9th iteration. 10th iteration returns disordered string. Here is my kernel:
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_amd_printf : enable
__kernel void l_system(int string_lenght){}
__kernel void l_system_interation(int string_lenght, __global char *sentence, __local char *string, __global int * local_char_num)
{
int local_x = (int)get_local_id(0);
int local_size = (int)get_local_size(0);
int x = (int)get_global_id(0);
int size = (int)get_global_size(0);
int group = (int)get_group_id(0);
int local_mem_index;
if(x < string_lenght){
//local mem index - offset for next group, copy char to local
local_mem_index = local_x * 5;
string[local_mem_index] = sentence[x];
if(local_x == 0){
//reset counter
atomic_xchg(&local_char_num[group], 0);
//atomic_add(&local_char_num[0], group);
}
}
barrier(CLK_LOCAL_MEM_FENCE);
barrier(CLK_GLOBAL_MEM_FENCE);
if(x < string_lenght){
if(string[local_mem_index] == 'A'){
atomic_add(&local_char_num[group], 5);
string[local_mem_index] = 'B';
string[local_mem_index + 1] = '(';
string[local_mem_index + 2] = 'A';
string[local_mem_index + 3] = ')';
string[local_mem_index + 4] = 'A';
}
else if(string[local_mem_index] == 'B'){
atomic_add(&local_char_num[group], 2);
string[local_mem_index + 1] = 'B';
//reset 3rd char of local_mem
string[local_mem_index + 2] = '0';
}
else{
atomic_add(&local_char_num[group], 1);
//reset 3rd char of local_mem
string[local_mem_index + 2] = '0';
string[local_mem_index + 2] = '0';
}
}
barrier(CLK_LOCAL_MEM_FENCE);
barrier(CLK_GLOBAL_MEM_FENCE);
//1 compute unit for every char from src
if(x < string_lenght){
//local first compute unit writes to result whole group string
if(local_x == 0){
int j = 0;
//find offset for write to result string
if(x != 0){
for(int l = 1;l <= group; l++)
{
j += atomic_xchg(&local_char_num[group-l], local_char_num[group-l]);
//if(l == 0)
}
atomic_xchg(&local_char_num[99+group], local_char_num[group]);
}
for(int i = 0; i < local_size; i++){
//only valid chars
if(string_lenght > (x+i)){
local_mem_index = i * 5;
//B rule, copy (,)
if(string[local_mem_index+2] != 'A'){
sentence[j++] = string[local_mem_index];
if(string[local_mem_index] == 'B'){
sentence[j++] = string[local_mem_index+1];
}
continue;//B,(,); next index;
}
else{ // A rule
sentence[j++] = string[local_mem_index];
sentence[j++] = string[local_mem_index+1];
sentence[j++] = string[local_mem_index+2];
sentence[j++] = string[local_mem_index+3];
sentence[j++] = string[local_mem_index+4];
}//if 'A'
//sentence[j] = 0;
}//if x+i
}//for
}// lx == 0
}
barrier(CLK_GLOBAL_MEM_FENCE);
}
I think, that something overflow anywhere, but can't find where... Maybe there is something wrong with my code in main:
cl_int letter_count = 0;
cl_int next_letter_count = 1;
for (int i = 0; i < iter_count; i++)
{
//printf("%s\n", sentence_init);
letter_count = next_letter_count;
next_letter_count = STRING_LENGTH_PAR((i + 1));
printf("in count: %d out count: %d\n", letter_count, next_letter_count);
CheckOpenCLError(clSetKernelArg(kernel_iteration, 0, sizeof(cl_int), &letter_count), "clSetKernelArg: letter_count");
CheckOpenCLError(clSetKernelArg(kernel_iteration, 2, sizeof(cl_char)* (local * RULE_SIZE + 1), NULL), "clSetKernelArg: tmp_string");
CheckOpenCLError(clEnqueueNDRangeKernel(queue, kernel_iteration, 1, NULL, &global, &local, 0, NULL, &kernel_iteration_event), "clEnqueueNDRangeKernel: kernel_iteration");
CheckOpenCLError(clFinish(queue), "clFinish");
kernel_computing_time += getEventTime(kernel_iteration_event);
}
CheckOpenCLError(clEnqueueReadBuffer(queue, sentence_dev, CL_TRUE, 0, sizeof(cl_char)* (next_letter_count), sentence_result, 0, NULL, &result_iteration_event), "clEnqueueReadBuffer: result_iteration_event");
cl_int *p = (cl_int*)malloc(sizeof(cl_int)*(STRING_LENGTH_PAR(iter_count)));
CheckOpenCLError(clEnqueueReadBuffer(queue, p_dev, CL_TRUE, 0, sizeof(cl_int)* (STRING_LENGTH_PAR(iter_count)), p, 0, NULL, &result_iteration_event), "clEnqueueReadBuffer: result_iteration_event");