以 C 为单位计算经过的时间



我是C的新手,我正在使用Linux。我被困在这个大学任务上。我需要评估程序,我需要制作一个程序来显示执行所经过的时间。这是起源程序。

/*
 * Simple MPI program to sum the numbers from 1 to 1000
 *
 * The output of this program should look like this:
 *
 * $ mpirun -n 4 ./su
 * node 1 of 4 starting at 251 and ending at 500
 * node 2 of 4 starting at 501 and ending at 750
 * node 3 of 4 starting at 751 and ending at 1000
 * node 0 of 4 starting at 1 and ending at 250
 * The sum from 1 to 1000 is: 500500
 *
 * Author: Kevan Buckley
 */
#include <stdio.h>
#include <mpi/mpi.h>
int main(int argc, char **argv) {
    // Rank is which process this code is executing in.
    // Rank 0 is the initial process.
    int rank;
    int size;
    int sum = 0;
    int start, end, accum, i;
    // These lines must appear at the start of any
    // MPI program.
    MPI_Status status;
    MPI_Init(&argc, &argv);
    // These lines tell MPI that rank and size are values
    // that we want to communicate between processes.
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    // Only compute part of the sum.
    // This splits the work over the different processes.
    start = (1000*rank/size)+1;
    end = 1000*(rank+1)/size;
    printf("node %d of %d start: %d end: at %dn",
           rank, size, start, end);
    for(i=start; i<=end; i++){
        sum = sum + i;
    }
    if(rank == 0) {
        for(i=1; i<size; i++){
            // Receive a message from another process.
            MPI_Recv(&accum, 1, MPI_INT, i, 1, 
                     MPI_COMM_WORLD,  &status);
            sum = sum + accum;
        }
        printf("The sum from 1 to 1000 is: %dn", sum );
    }
    else {
       // Send data to the process at rank 0.
       MPI_Send(&sum, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
    }
    // This code must end any MPI program.
    MPI_Finalize();
    return 0;
}

这是我的更新版本 - 要点。我从我正在处理的其他代码中获取了所有时间的计算。我尝试了很多东西,但仍然无法让它工作。

任何帮助都会有所帮助,谢谢!

如果我理解正确,您想为程序的一部分计时。下面是一个示例代码,用于使用 MPI_Wtime 进行并行编程。

#include <mpi.h>
double start, finish;
MPI_Init (&argc, &argv);
start=MPI_Wtime(); /*start timer*/
/*put the code you want to time here*/
finish=MPI_Wtime(); /*stop timer*/
MPI_Finalize();
printf("Parallel Elapsed time: %f secondsn", finish-start); 

最新更新