2 Replies Latest reply on Dec 19, 2011 6:01 AM by saibbot

    Round-robin shared memory access

    tsrawat

      Hello,

       

      I was wondering if the following was possible:

       

      2 cores,

      program starts,

      shared memory (shmem) is allocated (from off-chip DRAM)

      core 0 writes to shmem

      core 1 reads, then writes to shmem

      core 0 reads, then writes to shmem,

      --alternate until pre-specified condition--

      program exit

       

      This is what I have so far... but MPI-style programming is fairly new for me. So far this program compiles and executes, but hangs some instance after starting. I'm not sure what exactly I am doing - hence any guidance would be helpful.

       

      Thank you (code below)

       

      // Program for two or more cores to develop

      // sum using off chip shared memory

      #define MAX_SUM 10
      #include <string.h>
      #include <stdio.h>
      #include "RCCE.h"
      #include "RCCE_lib.h"

       

      int RCCE_APP(int argc, char *argv[])
      {
          RCCE_init(&argc, &argv);

       

          RCCE_shflush();
          RCCE_barrier(&RCCE_COMM_WORLD);

       

          int *ptr = NULL;
          ptr = (int *)RCCE_shmalloc(sizeof(int));

       

          if(!ptr) {
              printf("Could not allocated shared memory\n");
              return 0;
          }
          else {
              printf("Starting program...\n");
          }

       


          int ID;
          ID = RCCE_ue();

       

          while(*ptr < MAX_SUM) {
              if(*ptr == NULL && ID == 0) {
                  *ptr = 0;
                  RCCE_shflush();
                  printf("Core %d: - Sum: %d\n",ID,*ptr);
              }
              else {
                  RCCE_barrier(&RCCE_COMM_WORLD);
                  *ptr += 1;
                  RCCE_shflush();
                  printf("Core %d: - Sum: %d\n",ID,*ptr);
              }
          }

       

          if(ptr)
              RCCE_shfree((t_vcharp) ptr);
         
          RCCE_barrier(&RCCE_COMM_WORLD);
          RCCE_finalize();
          return 0;
      }

        • 1. Re: Round-robin shared memory access
          aprell

          Hi,

           

          I don't think this is possible without additional synchronization ... unless you don't mind if you lose some updates.

           

          The barrier inside the while loop could be a problem. Assume the counter has a value of MAX_SUM-2, core A increments it to MAX_SUM-1 and reenters the barrier, just before the increment of core B. Core B would then continue with the code following the loop.

           

          Also, I would suggest to initialize the counter right after allocation, for example:

           

          if (ID == 0)
              *ptr = 0;
           
          RCCE_barrier(&RCCE_COMM_WORLD);
          
          • 2. Re: Round-robin shared memory access
            saibbot

            I agree with Andreas; you need to initialize ptr before using it.

             

            Then, you compare *ptr (an integer) with NULL, hence you simply compare the value of the pointer with 0. Is this what you wanted to do?

             

            Moreover, as Andreas mentioned, since you use the barrier combined with conditional on the ID, your cores will not synchronize as intended.

             

            Finally, I suggest your use a barrier after the shmem allocation and initialization, or else the second initialization may erase some work from the other core.

             

            Vasilis.