Implementation of Eratosthenes (Eratosthenes) prime sieve parallelization using OpenMP

Source: Internet
Author: User

1. Algorithm Introduction 1.1 Sieve Method origin

Sieve method is a simple algorithm for verifying prime numbers. It is said that the ancient Greek Eratosthenes (Eratosthenes, about 274-194 BC) invented, also known as Eratosthenes sieve method (Sieve of Eratosthenes).

1.2 Sieve Process

the specific approach is:Give the range n to sieve the values to find out n √ The prime number within the P1,P2,P3,......,PK. From the smallest prime number 2 to sift, that is, 2 left, the multiples of 2 are removed, and then the next prime, that is, 3 sieve, 3 left, the multiples of 3 were removed; then the next prime number 5 sieve, 5 left, the multiples of 5 are removed;

2. Implementing the Code

The code is a Linux platform and can be easily modified to be ported to Windows. using OpenMP to achieve simple parallel acceleration, the use of OpenMP, Baidu Search "OpenMP simple tutorial."

#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <iostream>#include <sys/time.h>#include <cassert>#include <omp.h>using namespace STD;typedef unsigned intUInt32;typedef unsigned Long Long intUInt64;inline voidSieve (UInt64 start,uint64 end,uint64& num,intThreadnum) {assert (start>1);BOOL* A =New BOOL[end+1];memset(A +2,true, end+1);#pragma omp parallel for num_threads (threadnum)   for(UInt64 i =2; I <= (UInt64)sqrt(end); i++) {if(A[i]) for(UInt64 j = i; i*j <= end; j + +) A[i*j] =false; } UInt64 prime_num=0;if(start==2) prime_num++;#pragma omp parallel for num_threads (threadnum) reduction (+: Prime_num)   for(UInt64 i = (start%2==0? start+1: start); I <=end; i + =2)  {if(A[i]) prime_num++; } Num=prime_num;Delete[] A;}intMainintargcChar* argv[]) {if(argc!=4){fprintf(stderr,"Usage:eratosthenes start_number end_number threadnum\n");Exit(-1); }structTimeval Ts,te; UInt64 Start=atoi (argv[1]); UInt64 End=atoi (argv[2]);intThreadnum=atoi (argv[3]); UInt64 num=0;    Gettimeofday (&ts,null);    Sieve (Start,end,num,threadnum); Gettimeofday (&te,null);cout<<"Count:"<<num<<endl;cout<<"Total time:"<< ((te.tv_sec-ts.tv_sec) * ++ (TE.TV_USEC-TS.TV_USEC)/ +) <<"MS"<<endl; GetChar ();return 0;}
Reference documents

[1] Baidu Encyclopedia-sieve Method

Implementation of Eratosthenes (Eratosthenes) prime sieve parallelization using OpenMP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.