Search code examples
cconcurrencyprocesssparse-matrix

Running concurrent process in C and measure running time


I'm creating a program in C that loads a matrix from a file, divides it in N segments to be given to N processes.

Each process should count every 0 element in that segment of the matrix.

I should measure the time that it takes to the entire program to be completed. So, in theory if i add more processes it should take less time, in an inversely proportional meaning.

To do so, i measured the time of each process like this:

for (int i = 0; i < num_processes; ++i)
  {
    pid_t pid = fork();

    if (pid == -1)
    {
      perror("Fork failed");
      exit(EXIT_FAILURE);
    }

    if (pid == 0)
    {
      // Child process

      // Calculate the start and end rows for this process
      int start_row = i * segment_rows + (i < remaining_rows ? i : remaining_rows);
      int end_row = start_row + segment_rows + (i < remaining_rows ? 1 : 0);

      // Perform counting in the segment
      start = clock();
      long count = countZeros(matrix + start_row * columns, end_row - start_row, columns);
      end = clock();

      // Print the count and time taken by this process
      printf("Process %d Count: %ld Time: %f seconds\n", i, count, ((double)(end - start)) / CLOCKS_PER_SEC);

      exit(EXIT_SUCCESS);
    }
  }

When i run it with a 1000 x 2000 matrix, for example, the times for each process do reduce if i keep adding processes (The limit i'm setting is the amount of CPU cores available).

The problem is that, when i measure the entire program time like this:

// Wait for all child processes to finish
  start = clock();
  for (int i = 0; i < num_processes; ++i)
  {
    wait(NULL);
  }
  end = clock();
// Print the total time taken by all processes
  printf("Total time: %f seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);

The time now does not only increase but it doesn't even relate to the time of all the processes.

So the final question is, should the overall time be measured in other way or is this problem related to the concurrency management done in the program?

Here are some screenshots that show the problem described:

enter image description here

And here's the repo with the entire code: https://github.com/NicolasMonta1807/matrix-concurrency

If you're interested in more, this should be applied to a sparse matrix validator that is built here: https://github.com/NicolasMonta1807/sparse-matrix-validator . It is, in general, working and it does its job but the only problem is the stated in this post.


Solution

  • So, in the end it was the measure using clock(). I replaced it with clock_gettime(), as Weather Vane suggested in their comment, and it is working as expected.

    /**
         * ---------- TIMER ----------
         */
        struct timespec begin, end;
        clock_gettime(CLOCK_REALTIME, &begin);
    
        int total = 0;
    
        for (int i = 0; i < num_processes; ++i)
        {
          wait(NULL);
        }
    
        clock_gettime(CLOCK_REALTIME, &end);
        double time_spent = (end.tv_sec - begin.tv_sec) +
                            (end.tv_nsec - begin.tv_nsec) / BILLION;
        printf("Terminado en %f\n", total, time_spent);
    

    I'm really grateful for your help.