CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Storing and using variables on a specific processor

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 10, 2019, 02:40
Default Storing and using variables on a specific processor
  #1
Member
 
Ashish Magar
Join Date: Jul 2016
Location: Mumbai, India
Posts: 81
Rep Power: 9
ashishmagar600 is on a distinguished road
Hello guys,

I will describe my problem briefly.

In my modified code, I am doing certain computations (such as averaging and stuff) on some pipes. The mesh is huge and I wanted to decompose, such that the whole pipe section comes under one processor. This was done.

I wanted to declare some variables which are to be used only on the same processor as the pipe and to be accessed at different times during an outer iteration (maybe piso).

I was trying to do something like this:
Code:
       if(Pstream::MyProcNo() == 1){
             int a[100];
       }

//Some solving to be done everywhere

       if(Pstream::MyProcNo() == 1){
             a[50] = 5;
       }
Here, it would say
Code:
a is not declared in this scope
I have made sure that both the steps cannot be clubbed together, as somethings necessary are done in between.

Is there some way I can do this, or am I doing this completely wrong?

Thanks
ashishmagar600 is offline   Reply With Quote

Old   July 10, 2019, 04:53
Default
  #2
Member
 
Hosein
Join Date: Nov 2011
Location: Germany
Posts: 93
Rep Power: 14
einstein_zee is on a distinguished road
Quote:
Originally Posted by ashishmagar600 View Post
Hello guys,

I will describe my problem briefly.

In my modified code, I am doing certain computations (such as averaging and stuff) on some pipes. The mesh is huge and I wanted to decompose, such that the whole pipe section comes under one processor. This was done.

I wanted to declare some variables which are to be used only on the same processor as the pipe and to be accessed at different times during an outer iteration (maybe piso).

I was trying to do something like this:
Code:
       if(Pstream::MyProcNo() == 1){
             int a[100];
       }

//Some solving to be done everywhere

       if(Pstream::MyProcNo() == 1){
             a[50] = 5;
       }
Here, it would say
Code:
a is not declared in this scope
I have made sure that both the steps cannot be clubbed together, as somethings necessary are done in between.

Is there some way I can do this, or am I doing this completely wrong?

Thanks
Hey there,

The point is once your variable is defined enclosed in curly braces its lifetime will come to an end once the control reaches to the end of the brace. In case you define a variable like this you need to make sure that you finish your job with that extra variable inside that territory. But it seems you want to have it for later use. Hence, you may allocate dynamically your variable with the help of a small function giving the size of your variable to be zero for other procs and giving the desired size for your specific proc.
exp.
Code:
void assign(int& arraySize, int nproc, int procID)
{
    if(procID == 1) // assuming your case
    {
        arraySize = 100;
    }
    else
    {
        arraySize = 0;
    }
}
and then in your main code you may allocate the variable like this:
Code:
int arraySize;
assign(arraySize, nProc, procID);
int* a = new int[arraySize];
einstein_zee is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:58.