Graph coloring in for loop

Hello everyone,

I am using Petsc in parallel and I am trying to color my nodes into different colors according to their location in different processes. So I created a code in C:

char colr[16][30]= {
                        "pink1",
                        "yellow1",
                        "red",
                        "turquoise",
                        "sienna",
                        "white",
                        "green",
                        "purple",
                        "blue",
                        "grey",
                        "black",
                        "brown",
                        "darkgoldenrod3",
                        "tan",
                        "plum4",
                        "peachpuff1"
                    };
  
   
      FILE * fpdot;
   char filenamea[100];
   sprintf(filenamea,"/home/edaoktay/Desktop/matrices/dots/mat_n_%d_size_%d_lap_%d_cut_%d_tol_%g.dot",nev,siz,lap,cutt,toleps);
   fpdot = fopen (filenamea,"w");
   fprintf (fpdot, "digraph PL_of_n_%d_size_%d_lap_%d_cut_%d_tol_%g{\n node [shape=point]\n ",nev,siz,lap,cutt,toleps);
   
    for (j=0;j<nev;j++){  
      if (rank == j){
        for (i=0;i<vend-vstart;i++){ 
            printf("%d color %s for rank = %d\n",vecar[i],colr[j],rank);
            fprintf (fpdot, "%d [fillcolor = %s];\n",vecar[i],colr[j] ); 
        }
      }
  }
   
  fprintf (fpdot, "}\n\n\n");
  fclose (fpdot);

where nev is the number of processes, vecar is the string holding node number in the process, and its size is vend-vstart.

After running the code, the dot file was written for only for one process. In other words, for instance, normally, the nodes should be arranged like that (like I got from printf() function):

1 color pink1 for rank = 0
4 color pink1 for rank = 0
0 color yellow1 for rank = 1
2 color yellow1 for rank = 1
3 color yellow1 for rank = 1
5 color yellow1 for rank = 1
6 color yellow1 for rank = 1
7 color yellow1 for rank = 1
8 color yellow1 for rank = 1

however, in dot file:

0 [fillcolor = yellow1];
2 [fillcolor = yellow1];
3 [fillcolor = yellow1];
5 [fillcolor = yellow1];
6 [fillcolor = yellow1];
7 [fillcolor = yellow1];
8 [fillcolor = yellow1];

i.e. only in process number 1 was considered in the loop.

I couldn’t understand how printf function prints the whole nodes in the whole processes but fprintf prints only nodes in one process.

I was wondering where and what am I doing wrong? Since this is the first time I am writing a dot file, I couldn’t understand where the problem is.

Thanks!

Welcome to the forum!

It is hard to say what is going wrong without seeing the rest of your code. The section you’ve quoted appears to print exactly the same number of lines to both stdout and fpdot during the loop, so clearly something is already off. Maybe the first two “pink1” lines you’ve quoted are coming from earlier printf statements in your code?

Thank you for answering!

“pink1” lines are not coming from earlier printf statements since currently, I am using 2 processes and printf function prints its line for each process. It states that process 0 has 2 nodes: 1 and 4.

However, fprintf only prints one process’ nodes and their color. I know that this happening because" of the usage of “rank”, but I don’t know how to solve the problem.

I don’t know anything about PETSc and without your full code and the full output including filenames, it’s very hard to say what’s going wrong. Are the processes completely separate? If not, perhaps one of the files may be overwritten by the other or not properly closed because of shared data?