Mam pytanie co do poniższego kodu.
Już od dawna zauważyłem że std::thread pod MinGW działa tak jak jemu się chce, ale takie cuda to po raz pierwszy widzę.
Czemu poniższy kod przy ustawieniu: const unsigned ThreadCount=8;
- działa poprawnie
zaś po przestawieniu: const unsigned ThreadCount=2;
- wywala się z wyjątkiem, no chyba że włączę debuger :)
#include <iostream>
#include <thread>
using namespace std;
#define USE_THREAD
//
#define USE_DOUBLE
const unsigned PairCount=18;
const unsigned ThreadCount=2; // Przy 8 - działa poprawnie
const unsigned FiltrCount=64;
const unsigned DeadCount=128;
const unsigned ForwardCount=128;
const unsigned ResultCount=8;
const unsigned SavedCount=DeadCount+ForwardCount+ResultCount;
const unsigned ColumnCount=FiltrCount*2;
#ifdef USE_DOUBLE
struct MatrixData
{
double X[PairCount];
// and more ...
};
MatrixData SV[SavedCount+1]={{0}};
double TC[PairCount][ColumnCount][ColumnCount]={{{0}}};
double TR[PairCount][ColumnCount]={0};
double TY[ResultCount][ColumnCount][PairCount]={{{0}}};
#else
template<class T> struct VirtualTab
{
unsigned Size;
T value;
VirtualTab(unsigned Size,const T &value):Size(Size),value(value) { }
T &operator[](unsigned p)
{
if(p>=Size)
{
cout<<"A tu cie mam"<<endl;
cin.get();
}
return value;
}
};
struct MatrixData
{
VirtualTab<double> X;
// and more ...
MatrixData():X(PairCount,0) {}
};
VirtualTab<MatrixData> SV(SavedCount+1,MatrixData());
VirtualTab<VirtualTab<VirtualTab<double> > > TC(PairCount,VirtualTab<VirtualTab<double> >(ColumnCount,VirtualTab<double>(ColumnCount,0)));
VirtualTab<VirtualTab<double> > TR(PairCount,VirtualTab<double>(ColumnCount,0));
VirtualTab<VirtualTab<VirtualTab<double> > > TY(ResultCount,VirtualTab<VirtualTab<double> >(ColumnCount,VirtualTab<double>(PairCount,0)));
#endif
void proc(int t)
{
for(unsigned y=t;y<ColumnCount;y+=ThreadCount)
{
for(unsigned p=0;p<PairCount;++p)
{
double add=0;
#ifdef USE_DOUBLE
double *tcy=TC[p][y];
#else
VirtualTab<double> &tcy=TC[p][y];
#endif
for(unsigned x=0;x<ColumnCount;++x)
{
add+=tcy[x]*TR[p][x];
}
for(unsigned s=0;s<ResultCount;++s)
{
TY[s][y][p]+=add*(SV[s+DeadCount+ForwardCount].X[p]-SV[s+DeadCount].X[p]);
}
}
}
}
int main()
{
#ifdef USE_THREAD
thread th[ThreadCount];
#endif
unsigned RowCount=0;
while(RowCount<1024*1024*1024)
{
for(unsigned t=0;t<ThreadCount;++t)
{
#ifdef USE_THREAD
th[t]=thread(proc,t);
#else
proc(t);
#endif
}
if(!(++RowCount&0xFF))
{
cout<<'\r'<<RowCount;
cout.flush();
}
#ifdef USE_THREAD
for(unsigned t=0;t<ThreadCount;++t) th[t].join();
#endif
}
return 0;
}