c - MPI 和 C : loop through file of commands in file

标签 c file mpi pbs torque

您好,我想在 40 个 CPU 上并行化一个进程,其中每个作业运行 1 个进程。

我在我的集​​群上使用 C 和 MPI 以及 TORQUE (PBS) 调度程序。

这是我的脚本。

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>

const char FILE_NAME[] = "/home/foo/c/mpi/cov_test/test_command.txt";
const char WRK_DIR[] = "/home/foo/c/mpi/cov_test";
char comm[39][256];
int main(int argc, char **argv)
{
    int rank;
    int size;
    int count =0;
    FILE *in_file;
    char line[256];
    char *pos;

    in_file = fopen(FILE_NAME, "r");
    if (in_file == NULL) {
            printf("Cannot open %s\n", FILE_NAME);
            exit(8);
    }
    if(in_file)
    {
            while(fgets(line, sizeof(line), in_file))
            {       /* Remove newline at end of file     *
                     * Maybe not the best way to do it? *
                     * mpicc compiler throws an error   */
                    if((pos=strchr(line, '\n')) != NULL)
                            *pos = '\0';
                    strcpy(comm[count], line);

                    count++;
            }
    }

    chdir(WRK_DIR); 

    chdir(WRK_DIR);

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    /* Saw this on another SO posting */
    int start = (rank*count)/size;
    int end = ((rank+1)*count)/size;

    for(int i= start; i < end; i++) {

            printf("%s\n", comm[rank]);
            /*system(comm[rank]);*/
    }

    MPI_Finalize();
}

输出我只有文件“test_command.txt”的第一条命令

这是我的扭矩提交文件

#!/bin/bash
#PBS -q condo
#PBS -l walltime=01:00:00
#PBS -l nodes=5:ppn=8
#PBS -j oe
#PBS -o /home/foo/c/mpi/cov_test/pilot_mpi_out

#had to export my perl libraries
export PERL5LIB=/home/foo/myperl/lib/perl5:/home/foo/myperl/share/perl5:$PERL5LIB
cd $HOME/c/mpi/cov_test
/opt/openmpi/bin/mpirun -machinefile $PBS_NODEFILE -np 40 ./pilot_mpi_test-2

我是 C 的新手,所以我可能(可能)做了一些可怕的事情。感谢您的宝贵时间:)

最佳答案

应该是printf("%s\n", comm[i]);而不是printf("%s\n", comm[rank]);.

关于c - MPI 和 C : loop through file of commands in file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396348/

相关文章:

计算二维数组中的页面错误数

gcc - 如何用gcc编译MPI?

c - 关于解决并行化问题的一般问题

visual-studio-2012 - 使用 VS2012 进行 MPI 调试

c - 从老式测量仪器解码数据

c - 通过函数时获取垃圾而不是变量

python - 合并文件权限

c++ - QFileInfo 所有者和组为空

c - if 语句的数组元素

javascript - 每次事件后创建目录