OpenMPI C语言scanf不会停止接受输入(Mac OS X 10.10) [英] OpenMPI C language scanf does not stop taking input (Mac OS X 10.10)

查看:227
本文介绍了OpenMPI C语言scanf不会停止接受输入(Mac OS X 10.10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下MPI代码.问题出在scanf上.该命令将继续接受输入,而不是在任何地方.应该只接受一个输入字符串.

I am trying to run the following MPI code. The problem is with the scanf. The command keep on taking input and does not anywhere. It is supposed to take one input string only.

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

int main(int argc,char * argv[])
{
    int npes, myrank, length = 10;
    char string[length+1];      // array size changed to length +1 as suggested in comments.
    memset(string, 0, length+1);
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &npes);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    if (myrank == 0) {
        printf("Please enter the string of length %d or enter 0 to generate string randomly:\n", length);
        scanf ("%10s", string);     // format changed as suggested in comments
        printf("%s\n", string);
    }

    MPI_Finalize();
    return 0;
}

输出:

平台:Mac OS X 10.10

Platform: Mac OS X 10.10

MPI版本:打开MPI:1.8.3

MPI Version : Open MPI: 1.8.3

系统信息: Apple LLVM版本6.0(clang-600.0.54)(基于LLVM 3.5svn) 目标:x86_64-apple-darwin14.0.0 线程模型:posix

System Info: Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.0.0 Thread model: posix

如果我在某个地方写错了,请纠正我.

Please correct me if I am wrong somewhere.

推荐答案

由于将输入转发到所有进程的复杂性,在MPI程序中通常不建议从stdin读取.它不是可以在不同的实现之间移植的东西.

Reading from stdin is generally inadvisable in MPI programs due to the complexities of forwarding the input to all of the processes. It's not something that will be portable between different implementations.

通常,人们为应用程序获取输入的方式是读取输入文件.随处可见,您要做的就是使文件在所有进程中都可用.

Usually, the way people get input for their applications is to read input files. That works everywhere and all you have to do is make the file available at all processes.

这篇关于OpenMPI C语言scanf不会停止接受输入(Mac OS X 10.10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆