Like Tree1Likes
  • 1 Post By std10093

Variables on Specific ids (MPI)

This is a discussion on Variables on Specific ids (MPI) within the C Programming forums, part of the General Programming Boards category; I'm running a simple SPMD program with MPI. Is there a way to define special variables, defined on only some ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    13

    Variables on Specific ids (MPI)

    I'm running a simple SPMD program with MPI.
    Is there a way to define special variables, defined on only some of the ids. Lets say only for worker id 4.

    Code:
    MPI_Comm_rank (MPI_COMM_WORLD, &labindex);     
    /* get current process id */ 
    
    .....   
    
    if (labindex==4)
    {
        double V_mean[Ndx+1][tspc];
        int taym; 
    }
    
    .....
    
    if ((labindex==4) && (tsn>save_start) && (tsn<save_end))
    {
       taym=tsn%tspc;
       for (i=0;i<Ndx+1;i++)
       {
            V_mean[i][taym]=V_mean[i][taym]+V[i]/cta;
       }
    }
    
    .....
    Gives error when compiling.
    PPICC.c: In function ‘main’:
    PPICC.c:213: warning: unused variable ‘taym’
    PPICC.c:212: warning: unused variable ‘V_mean’
    PPICC.c:329: error: ‘taym’ undeclared (first use in this function)
    PPICC.c:329: error: (Each undeclared identifier is reported only once
    PPICC.c:329: error: for each function it appears in.)
    PPICC.c:332: error: ‘V_mean’ undeclared (first use in this function)


    If I define the variable V_mean for all of the workers, I will use excessive memory. Is there a solution for this?

    With Best Regards
    Last edited by Nooby 1 Kenooby; 11-10-2012 at 08:13 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    I do not know what MPI is but is there maybe a scope lifetime of the variables inside the loop you declared them?If so you could declare them once at top of the function. (But i do not know about MPI i repeat)

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    I got it now. I will define taym and V_mean, before I start the MPI. So there will be only 1 taym and V_mean not 80 (in my case).
    Although you don't know about MPI, you have a strong algorihm. Pure genius!! I'm trying it and looks like it will work. Will inform you about it.

    Thank you

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    I want to add something though

    Declare them at the top of the function, not as global variables!

    Why not global variables ( in general )?
    1) In a large program global variables may have the same name with local ones and thus we have a conflict, where the global is shadowed be the local one.This code is prune to logical errors( which are more difficult to detect than syntax ones)

    2) Decrease the readability of your code dramatically

    3) If you choose to declare a variable as global than pass it in functions as parameters, automatically these functions lose their generality.What i want to say, is that you can not reuse this function in future programs without modifying it or taking into consideration the global variables.

    Thank you for your nice words

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Thank you for the tips and tricks. I didn't define the V_mean matrix as global variable, as you said.

    Your idea is working. But now I got another problem:
    The matrix is 601*18182 in size. I try to save it with 2 nested for loops (with fopen, fprintf), and having problems when saving it to .dat or txt file. Data is huge and seems like the file can't store all the elements.

    Do you know a better way to store big matrixes to .dat or .txt file? Later on I need to plot this data on gnuplot or matlab.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting variables with an optimized, specific process
    By slotown in forum C Programming
    Replies: 8
    Last Post: 10-02-2012, 11:39 PM
  2. Replies: 6
    Last Post: 12-02-2009, 07:47 AM
  3. Run code on specific CPU?
    By pgzh in forum C Programming
    Replies: 3
    Last Post: 03-21-2008, 02:14 PM
  4. Replies: 3
    Last Post: 11-28-2006, 02:44 PM
  5. im looking for this specific web site...
    By revelation437 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-31-2003, 10:38 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21