Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.
1696 Discussions

Problem with parallel_reduce it is giving invalid memory access problem error

deependu
Beginner
560 Views

hi all,

I am trying to figure out for last 2 days why this code is not working. The code is giving me memory access error.

First let me explain , What the code is intended to do ?

I have got two vectors, nodeList and nodesToInsert .And I want to insert the nodes from nodesToInsert
to the finalNodeList vector.


checkNodeForInsertion() is a dummy function which here is just returning true. In actual system it consist of
logic which is working fine.

The final outcome of the program is to have all the nodes in finalNodesList vector.

The working code is as follows:-

#include "tbb/task_scheduler_init.h"
#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"
#include
#include
#include
using namespace tbb;
using namespace std;



class Parallel_StringFinder{

vector edgeList;
vector nodesToInsert;

public:
vector finalNodesList;

void operator()(const blocked_range& r ){
string stringTemp;
string edgeToInsert;
bool flag1=false,flag2=false,toInsert=false;

for(size_t I = r.begin(); I< r.end();++I)
{

edgeToInsert = nodesToInsert.at(I);

toInsert = false;
for (int it=0; it!=edgeList.size(); it++)
{
stringTemp = edgeList.at(it);

if(stringTemp.size()!=edgeToInsert.size())
{
flag1 = true;
continue;
}
else
{
flag2 = true;
flag1=false;
toInsert = checkNodeForInsertion(stringTemp,edgeToInsert);
if((toInsert)==0)
break;
}

}
if(toInsert){
finalNodesList.push_back(edgeToInsert);
cout<<"................Node inserted-->"<< }
if(flag1&&!flag2){
finalNodesList.push_back(edgeToInsert);
cout<<"................Node inserted-->"<< }

}

}
Parallel_StringFinder(vector _edgeList,vector _nodesToInsert,vector _finalNodeList):edgeList(_edgeList),nodesToInsert(_nodesToInsert),finalNodesList(_finalNodeList)
{}

Parallel_StringFinder(Parallel_StringFinder& stringFinder,split)
{
cout<<<"I am in split"< this->edgeList = stringFinder.edgeList;
this->nodesToInsert = stringFinder.nodesToInsert;
}

void join(const Parallel_StringFinder& stringFinder)
{
cout<<<"I am in join"< for(int i=0;i finalNodesList.push_back(stringFinder.finalNodesList.at(i));
}

bool checkNodeForInsertion(string temp1,string temp2)
{
return true;
}

};


int main()
{

task_scheduler_init init;
vector edgeList;

edgeList.push_back("GHQR");
edgeList.push_back("QRMN");
edgeList.push_back("KLMN");
edgeList.push_back("MNYZ");
edgeList.push_back("ABYZ");
edgeList.push_back("ABCD");
edgeList.push_back("CDYZ");

vector nodesToInsert;
nodesToInsert.push_back("KLMNABCD");
nodesToInsert.push_back("GHQRKLMN");


Parallel_StringFinder pf(edgeList,nodesToInsert,edgeList);


parallel_reduce(blocked_range(0,edgeList.size()),pf,auto_partitioner());
vector finalNodeList = pf.finalNodesList;

for(int i=0;i cout<
int i;
cout<<"Enter some value continue..."
cin>>i;


}



How the code is now behaving:-

When the code runs, it

Sometimes run fine.

But most of the times it gives me error

"Unhandled exception at 0x7c81eb33 in IntelCompetition.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f930.."

and lands up in the file task.h at following code snippet

static void spawn_root_and_wait( task& root ) {
__TBB_ASSERT( root.is_owned_by_current_thread(), "root not owned by current thread" );
this line---> root.prefix().owner->spawn_root_and_wait( root, root.prefix().next );
}



Please let me know if more infomation is needed



Thanks

With regards
Deependu



0 Kudos
1 Solution
Dmitry_Vyukov
Valued Contributor I
560 Views
Quoting - Dmitriy V'jukov

You are executing parallel_reduce on the range [0..7) (size of edgeList). Then inside Parallel_StringFinder::operator() you are using this range [0..7) to index into nodesToInsert array which is of size [0..2):

edgeToInsert = nodesToInsert.at(I);

Obviously, when I >= 2 nodesToInsert.at() function throws out_of_range exception.

Here is how you can track down such kind of errors in MSVC:

0. Run the program under debugger.

1. Check out "Output" window after execution. Output windows contains something like:

[...]

First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0085ba58..
First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..
Unhandled exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..

[...]

Notice there was canceled (First-chance exception) std::out_of_range exception. Which is probably the cause of the problem.

2. Then you go to the Debug->Exceptions window. There you must mark "std::exception" (this is the base class of std::out_of_range exception).

3. Run the program once again.

Not debugger will stop execution when first std::out_of_range exception is thrown.

In the Call Stack window you can see where exception occurs:

edgeToInsert = nodesToInsert.at(I);

Also you can see than I == 3, and nodesToInsert.size() == 2.

Further reasoning is straightforward.

View solution in original post

0 Kudos
3 Replies
Dmitry_Vyukov
Valued Contributor I
560 Views
Quoting - deependu

I am trying to figure out for last 2 days why this code is not working. The code is giving me memory access error.

You are executing parallel_reduce on the range [0..7) (size of edgeList). Then inside Parallel_StringFinder::operator() you are using this range [0..7) to index into nodesToInsert array which is of size [0..2):

edgeToInsert = nodesToInsert.at(I);

Obviously, when I >= 2 nodesToInsert.at() function throws out_of_range exception.

0 Kudos
Dmitry_Vyukov
Valued Contributor I
561 Views
Quoting - Dmitriy V'jukov

You are executing parallel_reduce on the range [0..7) (size of edgeList). Then inside Parallel_StringFinder::operator() you are using this range [0..7) to index into nodesToInsert array which is of size [0..2):

edgeToInsert = nodesToInsert.at(I);

Obviously, when I >= 2 nodesToInsert.at() function throws out_of_range exception.

Here is how you can track down such kind of errors in MSVC:

0. Run the program under debugger.

1. Check out "Output" window after execution. Output windows contains something like:

[...]

First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0085ba58..
First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..
Unhandled exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..

[...]

Notice there was canceled (First-chance exception) std::out_of_range exception. Which is probably the cause of the problem.

2. Then you go to the Debug->Exceptions window. There you must mark "std::exception" (this is the base class of std::out_of_range exception).

3. Run the program once again.

Not debugger will stop execution when first std::out_of_range exception is thrown.

In the Call Stack window you can see where exception occurs:

edgeToInsert = nodesToInsert.at(I);

Also you can see than I == 3, and nodesToInsert.size() == 2.

Further reasoning is straightforward.

0 Kudos
deependu
Beginner
560 Views
Quoting - Dmitriy V'jukov

Here is how you can track down such kind of errors in MSVC:

0. Run the program under debugger.

1. Check out "Output" window after execution. Output windows contains something like:

[...]

First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0085ba58..
First-chance exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..
Unhandled exception at 0x7c812aeb (kernel32.dll) in static_tbb.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012f440..

[...]

Notice there was canceled (First-chance exception) std::out_of_range exception. Which is probably the cause of the problem.

2. Then you go to the Debug->Exceptions window. There you must mark "std::exception" (this is the base class of std::out_of_range exception).

3. Run the program once again.

Not debugger will stop execution when first std::out_of_range exception is thrown.

In the Call Stack window you can see where exception occurs:

edgeToInsert = nodesToInsert.at(I);

Also you can see than I == 3, and nodesToInsert.size() == 2.

Further reasoning is straightforward.

Hello Dmitriv V'jukov sir,

Thanks for your kind help and the way to setup the workbench. I was so indulge in problem that I was not able to think of this mistake.

Thanks once again sir.

Please keep correcting me in future.

With regards

Deependu

0 Kudos
Reply