在OpenMP中实现Euler Totient函数

时间:2016-10-26 07:36:19

标签: c++ openmp

我是OpenMP的新手,我正在尝试在OpenMP中实现Euler Totient功能。 这是我第一次尝试在OpenMP中编码。我的目标是并行实现它(如果可能的话,最快),然后按顺序实现。之后我计算了加速和效率。 以下是我的代码:

/*
 ============================================================================
 Name        : 55390_ass3.cpp
 Author      : Kamil Kamili
 Version     :
 Copyright   : Your copyright notice
 Description : Euler’s Totient function φ(n) in OpenMP
 ============================================================================
 */
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int eulerTotient(int number)
{
        int sNumber = number;
        for(int i = 2; i*i <= number; ++i)
        {
                if(number % i == 0)
                {
                        while(number % i == 0)
                        {
                                number /= i;
                        }
                }
        }
        if(number > 1)
        {
                sNumber -= sNumber / number;
        }
        return sNumber;
}

int main (int argc, char *argv[])
{
        int nTheads = omp_get_num_threads();
        int nMaxThreads = omp_get_max_threads();
        printf("Parallel Execution: \n");
        double startTimeForParallel = omp_get_wtime();
#pragma omp parallel for
         for(int i = 2; i < 10000; i++)
         {
                 printf("φ(%d) = %d\n", i, eulerTotient(i));
         }
         double parallelTime = omp_get_wtime() - startTimeForParallel;
         printf("Sequential Execution: \n");
         double startTimeForSequential = omp_get_wtime();
         for(int i = 2; i < 10000; i++)
                  {
                          printf("φ(%d) = %d\n", i, eulerTotient(i));
                  }
         double sequentialTime = omp_get_wtime() - startTimeForSequential;
         double speedUp = sequentialTime/parallelTime;
         printf("Parallel Execution Time: ",parallelTime);
         printf("Sequential Execution Time: " << sequentialTime);
         printf("SpeedUp: "<<speedUp);
 return 0;
}

但它不计算&#39;平行时间&#39;。知道我哪里错了吗?

所以我在做了一些研究后,在这里和那里编辑了我的代码。我想我没有正确使用函数omp_get_wtime()

按编辑代码:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
double omp_get_wtime(void);

int eulerTotient(int number)
{
        int sNumber = number;
        for(int i = 2; i*i <= number; ++i)
        {
                if(number % i == 0)
                {
                        while(number % i == 0)
                        {
                                number /= i;
                        }
                }
        }
        if(number > 1)
        {
                sNumber -= sNumber / number;
        }
        return sNumber;
}

int main (int argc, char *argv[])
{
        int nTheads = omp_get_num_threads();
        int nMaxThreads = omp_get_max_threads();
        printf("Parallel Execution: %f\n");
        double parallelTime, endTimeForParallel;
        double startTimeForParallel = omp_get_wtime();
#pragma omp parallel for
         for(int i = 2; i < 10000; i++)
         {
                 printf("O(%d) = %d\n", i, eulerTotient(i));
         }
         endTimeForParallel = omp_get_wtime();
         parallelTime = startTimeForParallel = endTimeForParallel;


        printf("Sequential Execution: \n");
        double sequentialTime,endTimeForSequential,startTimeForSequential = omp_get_wtime();
        for(int i = 2; i < 10000; i++)
        {
                printf("O(%d) = %d\n", i, eulerTotient(i));
        }
        endTimeForSequential = omp_get_wtime();
        sequentialTime = startTimeForSequential - endTimeForSequential;
        printf("Parallel Execution Time: %f seconds\n", parallelTime);
        printf("Sequential Execution Time: %f seconds\n", sequentialTime);
        double speedUp = sequentialTime/parallelTime;
        printf("SpeedUp: %f\n\n",speedUp);
        return 0;
}

输出简直奇怪。无论如何,这里是:

Parallel Execution Time: 1477476938.765000 seconds
Sequential Execution Time: -0.099000 seconds
SpeedUp: -0.000000

所以这是一个非常棒的错误:修正了

正如@Gilles所述。我修复了我的代码。这是:

/*
 ============================================================================
 Name        : 55390_ass3.c
 Author      : Kamil Kamili
 Version     :
 Copyright   : Your copyright notice
 Description : Euler Totient's function in C
 ============================================================================
 */
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
double omp_get_wtime(void);

int eulerTotient(int number)
{
        int sNumber = number;
        for(int i = 2; i*i <= number; ++i)
        {
                if(number % i == 0)
                {
                        while(number % i == 0)
                        {
                                number /= i;
                        }
                }
        }
        if(number > 1)
        {
                sNumber -= sNumber / number;
        }
        return sNumber;
}

int main (int argc, char *argv[])
{
        int nTheads = omp_get_num_threads();
        int nMaxThreads = omp_get_max_threads();
        printf("Parallel Execution: %f\n");
        double parallelTime, endTimeForParallel;
        double startTimeForParallel = omp_get_wtime();
#pragma omp parallel for
         for(int i = 2; i < 10000; i++)
         {
                 printf("O(%d) = %d\n", i, eulerTotient(i));
         }
         endTimeForParallel = omp_get_wtime();
         parallelTime = endTimeForParallel - startTimeForParallel;


        printf("Sequential Execution: \n");
        double sequentialTime,endTimeForSequential,startTimeForSequential = omp_get_wtime();
        for(int i = 2; i < 10000; i++)
        {
                printf("O(%d) = %d\n", i, eulerTotient(i));
        }
        endTimeForSequential = omp_get_wtime();
        sequentialTime = endTimeForSequential - startTimeForSequential;
        printf("Parallel Execution Time: %f seconds\n", parallelTime);
        printf("Sequential Execution Time: %f seconds\n", sequentialTime);
        double speedUp = sequentialTime/parallelTime;
        printf("SpeedUp: %f\n\n",speedUp);
        return 0;
}

输出:

Parallel Execution Time: 0.014000 seconds
Sequential Execution Time: 0.095000 seconds
SpeedUp: 6.785746

我仍然认为有些事情是错的,除非别人告诉我。

0 个答案:

没有答案