<i id='fBZYu'><tr id='fBZYu'><dt id='fBZYu'><q id='fBZYu'><span id='fBZYu'><b id='fBZYu'><form id='fBZYu'><ins id='fBZYu'></ins><ul id='fBZYu'></ul><sub id='fBZYu'></sub></form><legend id='fBZYu'></legend><bdo id='fBZYu'><pre id='fBZYu'><center id='fBZYu'></center></pre></bdo></b><th id='fBZYu'></th></span></q></dt></tr></i><div id='fBZYu'><tfoot id='fBZYu'></tfoot><dl id='fBZYu'><fieldset id='fBZYu'></fieldset></dl></div>

      <small id='fBZYu'></small><noframes id='fBZYu'>

        <bdo id='fBZYu'></bdo><ul id='fBZYu'></ul>
        <legend id='fBZYu'><style id='fBZYu'><dir id='fBZYu'><q id='fBZYu'></q></dir></style></legend>

      1. <tfoot id='fBZYu'></tfoot>

        如何使用g++编译openmp

        How to compile openmp using g++(如何使用g++编译openmp)

        <small id='QcuGq'></small><noframes id='QcuGq'>

          <tbody id='QcuGq'></tbody>

        1. <tfoot id='QcuGq'></tfoot>
            <bdo id='QcuGq'></bdo><ul id='QcuGq'></ul>

                • <i id='QcuGq'><tr id='QcuGq'><dt id='QcuGq'><q id='QcuGq'><span id='QcuGq'><b id='QcuGq'><form id='QcuGq'><ins id='QcuGq'></ins><ul id='QcuGq'></ul><sub id='QcuGq'></sub></form><legend id='QcuGq'></legend><bdo id='QcuGq'><pre id='QcuGq'><center id='QcuGq'></center></pre></bdo></b><th id='QcuGq'></th></span></q></dt></tr></i><div id='QcuGq'><tfoot id='QcuGq'></tfoot><dl id='QcuGq'><fieldset id='QcuGq'></fieldset></dl></div>
                • <legend id='QcuGq'><style id='QcuGq'><dir id='QcuGq'><q id='QcuGq'></q></dir></style></legend>
                  本文介绍了如何使用g++编译openmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有关于 openmp 编译的问题.

                  I have a problem about the openmp compiling.

                  像下面的代码:

                  #include <iostream> 
                  #include <pthread.h>
                  #include <omp.h>
                  #include <semaphore.h>
                  #include <stack>
                  using namespace std;
                  sem_t empty,full;
                  stack<int> stk;
                  void produce(int i)
                  {
                      {
                      sem_wait(&empty);
                              cout<<"produce "<<i*i<<endl;
                              stk.push(i*i);
                      sem_post(&full);
                      }
                  }
                  void consume1(int &x)
                  {
                      sem_wait(&full);
                              int data=stk.top();
                              stk.pop();
                              x=data;
                      sem_post(&empty);
                  }
                  void consume2()
                  {
                      sem_wait(&full);
                              int data=stk.top();
                              stk.pop();
                              cout<<"consume2 "<<data<<endl;
                      sem_post(&empty);
                  }
                  int main()
                  {
                      sem_init(&empty,0,1);
                      sem_init(&full,0,0);
                      pthread_t t1,t2,t3;
                      omp_set_num_threads(3);
                      int TID=0;
                      #pragma omp parallel private(TID)
                      {
                              TID=omp_get_thread_num();
                              if(TID==0)
                              {
                              cout<<"There are "<<omp_get_num_threads()<<" threads"<<endl;
                              for(int i=0;i<5;i++)
                                      produce(i);
                              }
                              else if(TID==1)
                              {
                                      int x;
                                      while(true)
                                      {
                                              consume1(x);
                                              cout<<"consume1 "<<x<<endl;
                                      }
                              }
                              else if(TID==2)
                              {
                                      int x;
                                      while(true)
                                      {
                                              consume1(x);
                                              cout<<"consume2 "<<x<<endl;
                                      }
                              }
                      }
                      return 0;
                  }
                  

                  首先,我使用:

                  g++ test.cpp -fopenmp -lpthread
                  

                  而且,我得到了正确的答案,总共有 3 个线程.

                  And, I got the right answer, there are 3 threads totally.

                  但是,当我像这样编译时:

                  But, when I do the compile like this:

                  g++ -c test.cpp -o test.o
                  g++ test.o -o test -fopenmp -lpthread
                  

                  只有一个线程.

                  谁能告诉我如何正确编译这段代码.提前谢谢你.

                  Anyone can tell me how to compile this code correctly. Thankyou in advance.

                  推荐答案

                  OpenMP 是一组代码转换 pragma,即它们仅在编译时应用.您不能将代码转换应用于已编译的目标代码(好吧,您可以,但它涉及的过程要多得多,并且超出了当今大多数编译器所做的范围).您在链接阶段需要 -fopenmp 仅用于编译器自动链接 OpenMP 运行时库 libgomp - 它不会对目标代码执行任何其他操作.

                  OpenMP is a set of code transforming pragmas, i.e. they are only applied at compile time. You cannot apply code transformation to an already compiled object code (ok, you can, but it is far more involving process and outside the scope of what most compilers do these days). You need -fopenmp during the link phase only for the compiler to automatically link the OpenMP runtime library libgomp - it does nothing else to the object code.

                  顺便说一句,尽管在技术上是正确的,但您的代码以非常非 OpenMP 的方式执行 OpenMP.首先,您重新实现了 OpenMP sections 构造.main 函数中的并行区域可以用更 OpenMP 的方式重写:

                  On a side note, although techically correct, your code does OpenMP in a very non-OpenMP way. First, you have reimplemented the OpenMP sections construct. The parallel region in your main function could be rewritten in a more OpenMP way:

                  #pragma omp parallel sections
                  {
                      #pragma omp section
                      {
                          cout<<"There are "<<omp_get_num_threads()<<" threads"<<endl;
                          for(int i=0;i<5;i++)
                              produce(i);
                      }
                      #pragma omp section
                      {
                          int x;
                          while(true)
                          {
                              consume1(x);
                              cout<<"consume1 "<<x<<endl;
                          }
                      }
                      #pragma omp section
                      {
                          int x;
                          while(true)
                          {
                              consume1(x);
                              cout<<"consume2 "<<x<<endl;
                          }
                      }
                  }
                  

                  (如果您在使用超过三个 OpenMP 线程运行此代码时获得 SIGILL,则您遇到了 GCC 中的错误,将在即将发布的版本中修复)

                  (if you get SIGILL while running this code with more than three OpenMP threads, you have encountered a bug in GCC, that will be fixed in the upcoming release)

                  其次,您可能想看看 OpenMP task 构造.有了它,您可以将要由任何空闲线程作为任务并发执行的代码段排队.不幸的是,它需要一个支持 OpenMP 3.0 的编译器,从等式中排除 MSVC++,但前提是您关心 Windows 的可移植性(而您显然不关心,因为您使用的是 POSIX 线程).

                  Second, you might want to take a look at OpenMP task construct. With it you can queue pieces of code to be executed concurrently as tasks by any idle thread. Unfortunately it requires a compiler which supports OpenMP 3.0, which rules out MSVC++ from the equation, but only if you care about portability to Windows (and you obviously don't, because you are using POSIX threads).

                  这篇关于如何使用g++编译openmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                    <bdo id='r7tiy'></bdo><ul id='r7tiy'></ul>

                    <small id='r7tiy'></small><noframes id='r7tiy'>

                      <tbody id='r7tiy'></tbody>
                  • <legend id='r7tiy'><style id='r7tiy'><dir id='r7tiy'><q id='r7tiy'></q></dir></style></legend>
                            <i id='r7tiy'><tr id='r7tiy'><dt id='r7tiy'><q id='r7tiy'><span id='r7tiy'><b id='r7tiy'><form id='r7tiy'><ins id='r7tiy'></ins><ul id='r7tiy'></ul><sub id='r7tiy'></sub></form><legend id='r7tiy'></legend><bdo id='r7tiy'><pre id='r7tiy'><center id='r7tiy'></center></pre></bdo></b><th id='r7tiy'></th></span></q></dt></tr></i><div id='r7tiy'><tfoot id='r7tiy'></tfoot><dl id='r7tiy'><fieldset id='r7tiy'></fieldset></dl></div>

                            <tfoot id='r7tiy'></tfoot>