并行计算–求半球体积openmp,mpi,java,c++,linux,.net实现
6.1 基于MPI的并行程序设计
6.1.1 代码及注释
#include “stdafx.h”
#include “time.h”
//using namespace std;
#define random(x) (rand()%x)
#define PI 3.1415926535898
void chuanxing(int wz_n,double wz_r){//串?行D时骸?间?计?算?模£块é
int wz_s;
double wz_ans=0,wz_duration;
clock_t wz_start, wz_finish;
wz_start = clock();
for(wz_s=1;wz_s<wz_n;wz_s++){
wz_ans+=2*wz_s-wz_s*wz_s/wz_n;
}
wz_ans=wz_ans*PI*wz_r*wz_r*wz_r/wz_n/wz_n;
wz_finish = clock();
wz_duration = (double)(wz_finish -wz_start) / CLOCKS_PER_SEC;
// cout<<ans<<endl;
//cout<<duration<<“串?行D时骸?间?:阰”<<duration<<endl;
printf( “%f 串?行D结á果?\n”, wz_ans );
printf( “%f seconds\n”, wz_duration );
}
int main(int argc, char *argv[])
{
clock_t wz_start, wz_finish;
double wz_duration,wz_ans,wz_r=5,wz_myans,wz_sum;
int wz_i,wz_n=100000000,wz_s,wz_npes,wz_myrank;
chuanxing(wz_n,wz_r);
wz_start = clock();
MPI_Init(&argc,&argv);//初?始?化ˉ
MPI_Comm_size(MPI_COMM_WORLD,&wz_npes);
MPI_Comm_rank(MPI_COMM_WORLD,&wz_myrank);
MPI_Bcast(&wz_n,1,MPI_INT,0,MPI_COMM_WORLD);//N值μ
wz_ans=0;
if(wz_n!=0){
for(wz_s=wz_myrank+1;wz_s<=wz_n;wz_s+=wz_npes){//根ù据Y起e始?ID确ā?定¨WZ_S值μ,?每?次?加ó上?线?程ì数簓
wz_ans+=2*wz_s-wz_s*wz_s/wz_n;
}
}
wz_myans =wz_ans*PI*wz_r*wz_r*wz_r/wz_n/wz_n;//求ó半?球ò和í
MPI_Reduce(&wz_myans,&wz_sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if(wz_myrank==0){
wz_finish = clock();
wz_duration = (double)(wz_finish – wz_start) / CLOCKS_PER_SEC;
printf( “%f 并¢行D结á果?\n”, wz_sum );
printf( “%f seconds\n”, wz_duration );
}
MPI_Finalize();
//system(“pause”);
return 0;
}
6.1.2 执行结果截图
加速比:1.8539
6.2 基于OPENNMP的并行程序设计
6.1.1 代码及注释
// openMP1.cpp : 定¨义?控?制?台?应畖用?程ì序ò的?入?口ú点?。£
//
#include “stdafx.h”
#include “time.h”
#include<string>
#define random(x) (rand()%x)
#define PI 3.1415926535898
void chuanxing(int wz_n,double wz_r){
int wz_s;
double wz_ans=0,wz_duration;
clock_t wz_start, wz_finish;
wz_start = clock();
for(wz_s=1;wz_s<=wz_n;wz_s++){
wz_ans+=(2*wz_s-wz_s*wz_s/wz_n);
}
//cout<<ans<<endl;
wz_ans=wz_ans*PI/wz_n*wz_r*wz_r*wz_r/wz_n;
wz_finish = clock();
wz_duration = (double)(wz_finish – wz_start) / CLOCKS_PER_SEC;
cout<<wz_ans<<endl;
cout<<wz_duration<<“串行时间:”<<wz_duration<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t wz_start, wz_finish;
double wz_duration,wz_ans,wz_r=5;
int wz_i,wz_j,wz_n=1000000000,wz_k,wz_temp,wz_num,wz_s;
//#pragma omp parallel num_threads(4)
chuanxing(wz_n,wz_r);
wz_ans=0;
wz_start = clock();
omp_set_num_threads(2);
#pragma omp parallel for reduction(+:wz_ans)
for(wz_s=1;wz_s<wz_n;wz_s++){
wz_ans+=2*wz_s-wz_s*wz_s/wz_n;
}
wz_ans=wz_ans*PI*wz_r*wz_r*wz_r/wz_n/wz_n;
wz_finish = clock();
wz_duration = (double)(wz_finish – wz_start) / CLOCKS_PER_SEC;
cout<<wz_ans<<endl;
printf( “%f seconds\n”, wz_duration );
system(“pause”);
return 0;
}
6.2.2 执行结果
加速比:1.867
6.3 基于Java的并行程序设计
6.3.1 代码及注释
package ball;
import java.text.*;
public class ball extends Thread{
private int wz_start;
private int wz_n;
private int wz_r;
private double wz_sum=0;
private double PI=3.1415926535898;
public ball(int wz_start,int wz_n,int wz_r){
super();
this.wz_start=wz_start;
this.wz_n=wz_n;
this.wz_r=wz_r;
}
public void run(){
for (int wz_s = wz_start; wz_s < wz_n; wz_s+=2)
{
wz_sum += 2 * wz_s – wz_s * wz_s / wz_n;
//System.out.println(“parallelsss=”+sum);
}
wz_sum = wz_sum * PI * wz_r * wz_r * wz_r / wz_n / wz_n;
}
public double sum(){
double wz_ans = 0;
for (int wz_s = 1; wz_s < wz_n; wz_s++)
{
wz_ans += 2 * wz_s – wz_s * wz_s / wz_n;
}
wz_ans = wz_ans * PI * wz_r * wz_r * wz_r / wz_n / wz_n;
return wz_ans;
}
public double getSum(){
return wz_sum;
}
public static void main(String [] args) throws InterruptedException{
int wz_n=500000000;
int wz_r=5;
ball wz_thread1=new ball(1,n,r);
ball wz_thread2=new ball(2,n,r);
long wz_startTime=System.currentTimeMillis();
wz_thread1.start();
wz_thread2.start();
wz_thread1.join();
wz_thread2.join();
long wz_endTime=System.currentTimeMillis();
double wz_t=(wz_thread1.getSum()+wz_thread2.getSum());
System.out.println(“²¢Ðнá¹û=”+wz_t);
System.out.println(“²¢ÐÐʱ¼ä=”+(wz_endTime-wz_startTime));
startTime=System.currentTimeMillis();
ball wz_serial=new ball(1,n,r);
double wz_tt=serial.sum();
wz_endTime=System.currentTimeMillis();
System.out.println(“´®Ðнá¹û=”+wz_tt);
System.out.println(“´®ÐÐʱ¼ä =”+(wz_endTime-wz_startTime));
}
}
6.3.2 执行结果
加速比:1.800
6.4 基于Windows API的并行程序设计
6.4.1 代码及注释
#include “stdafx.h”
#include<iostream>
#include<process.h>
#include<Windows.h>
#include “time.h”
using namespace std;
#define random(x) (rand()%x)
#define PI 3.1415926535898
double wz_duration,wz_ans=0,wz_r=5,wz_ans1=0,wz_ans2=0;
HANDLE wz_ev1,wz_ev2;
void chuanxing(int wz_n,double wz_r){
int wz_s;
double wz_ans=0,wz_duration;
clock_t wz_start, wz_finish;
wz_start = clock();
for(wz_s=1;wz_s<wz_n;wz_s++){
wz_ans+=2*wz_s-wz_s*wz_s/wz_n;
}
//cout<<ans<<endl;
wz_ans=wz_ans*PI*wz_r*wz_r*wz_r/wz_n/wz_n;
cout<<wz_ans<<endl;
wz_finish = clock();
wz_duration = (double)(wz_finish – wz_start) / CLOCKS_PER_SEC;
cout<<wz_duration<<“串?行D时骸?间?:阰”<<wz_duration<<endl;
}
int wz_n=1000000000;
int wz_i,wz_a[100000],wz_j,wz_k,wz_temp;
DWORD WINAPI x1(PVOID param){
int wz_s;
for(wz_s=1;wz_s<wz_n;wz_s+=2){
wz_ans1+=2*wz_s-wz_s*wz_s/wz_n;
}
SetEvent(wz_ev1);
return 0;
}
DWORD WINAPI x2(PVOID param){
int wz_s;
for(wz_s=2;wz_s<wz_n;wz_s+=2){
wz_ans2+=2*wz_s-wz_s*wz_s/wz_n;
}
SetEvent(wz_ev2);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t wz_start, wz_finish;
chuanxing(wz_n,wz_r);
wz_ans=0;
wz_ev1=CreateEvent(NULL,FALSE,FALSE,NULL);
wz_ev2=CreateEvent(NULL,FALSE,FALSE,NULL);
wz_start = clock();
HANDLE wz_th1=CreateThread(NULL,0,x1,NULL,0,NULL);
HANDLE wz_th2=CreateThread(NULL,0,x2,NULL,0,NULL);
WaitForSingleObject(wz_ev1,INFINITE);
WaitForSingleObject(wz_ev2,INFINITE);
//cout<<ans1<<” “<<ans2;
wz_ans=wz_ans1+wz_ans2;
//cout<<ans<<endl;
wz_ans=wz_ans*PI*wz_r*wz_r*wz_r/wz_n/wz_n;
wz_finish = clock();
wz_duration = (double)(wz_finish – wz_start) / CLOCKS_PER_SEC;
cout<<wz_ans<<endl;
printf( “%f seconds\n”, wz_duration );
system(“pause”);
return 0;
}
6.4.2 执行结果
加速比:1.59
6.5 基于.net的并行程序设计
6.5.1 代码及注释
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
int wz_r = 5;
int wz_n = 16000000;
Stopwatch stopwatch = new Stopwatch();
Work wz_work1 = new Work(1, wz_n, wz_r);
ThreadStart wz_thread1 = new ThreadStart(wz_work1.pSumto);
Thread wz_newthread1 = new Thread(wz_thread1);
Work wz_work2 = new Work(2, wz_n, wz_r);
ThreadStart wz_thread2 = new ThreadStart(wz_work2.pSumto);
Thread wz_newthread2 = new Thread(wz_thread2);
stopwatch.Start();
wz_newthread1.Start();
wz_newthread2.Start();
wz_newthread1.Join();
wz_newthread2.Join();
stopwatch.Stop();
TimeSpan timeSpan = stopwatch.Elapsed;
double milliseconds = timeSpan.TotalMilliseconds;
Console.WriteLine(“parallel sum={0}”, (wz_work1.getSum() + wz_work2.getSum()));
Console.Write(“parallel time=”);
Console.WriteLine(milliseconds);
stopwatch.Start();
Console.WriteLine(“serial sum={0}”, new Work(1, wz_n, wz_r).sumto());
stopwatch.Stop();
TimeSpan timeSpan2 = stopwatch.Elapsed;
double milliseconds2 = timeSpan2.TotalMilliseconds;
Console.Write(“serial time=”);
Console.Write(milliseconds2);
Console.Read();
}
}
class Work
{
private double sum = 0;
private int start;
private int n;
private int r;
private double PI=3.1415926535898;
public Work(int i,int n,int r)
{
this.start = i;
this.n = n;
this.r = r;
}
public void pSumto()
{
for (int s = start; s < n; s+=2)
{
sum += 2 * s – s * s / n;
}
sum = sum * PI * r * r * r / n / n;
}
public double sumto()
{
double ans = 0;
for (int s = 1; s < n; s++)
{
ans += 2 * s – s * s / n;
}
ans = ans * PI * r * r * r / n / n;
return ans;
}
public double getSum()
{
return sum;
}
}
6.5.2 执行结果截图
加速比:2.01
6.6 基于Linux的并行程序设计(选作)
6.6.1 代码及注释
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#define PI 3.1415926535898
#define wz_r 5
#define wz_n 100000000
void *bingxing(void *);
void *chuanxing(void *);
inline double WallTime();
double wz_ans[10];
int main(void)
{
int wz_i;
double wz_ans1;
double wz_time_start, wz_time_end;
pthread_t wz_p_threads[10];
wz_time_start = WallTime();
for (wz_i = 0;wz_i < 2; ++wz_i)
{
int *wz_threadNum =malloc(4);
*wz_threadNum=wz_i;
//printf(“threadNum is:%d\n”,*threadNum);
//printf(“start= %d\n”,start);
pthread_create(&wz_p_threads[wz_i],NULL, bingxing , (void *)wz_threadNum);
}
for(wz_i=0; wz_i<2; wz_i++)
{
pthread_join(wz_p_threads[wz_i], NULL);
wz_ans1 += wz_ans[wz_i];
}
wz_time_end = WallTime();
double wz_t1=wz_time_end-wz_time_start;
printf(“pi= %.2lf\n”,wz_ans1);
printf(“parallel time= %lf\n”, wz_t1);
wz_time_start = WallTime();
pthread_create(&wz_p_threads[2], NULL, chuanxing , (void *)3);
pthread_join(wz_p_threads[2], NULL);
wz_time_end = WallTime();
double wz_t2=wz_time_end-wz_time_start;
printf(“pi= %.2lf\n”,wz_ans[3]);
printf(“serial time= %lf\n”, wz_t2);
printf(“t2/t1=%lf\n”,wz_t2/wz_t1);
return 0;
}
void *bingxing(void * a)
{
int wz_start= *((int *)a);
int wz_s;
double wz_sum=0;
printf(“start= %d\n”,wz_start);
for (wz_s = wz_start; wz_s < wz_n; wz_s+=2)
{
wz_sum += 2 * wz_s – wz_s * wz_s / wz_n;
}
wz_sum = wz_sum * PI *wz_r *wz_r * wz_r /wz_n / wz_n;
wz_ans[wz_start]=wz_sum;
pthread_exit(0);
}
void *chuanxing(void * a)
{
double wz_sum = 0;
int wz_s ;
for (wz_s = 1; wz_s < wz_n; wz_s++)
{
wz_sum += 2 * wz_s – wz_s * wz_s / wz_n;
}
wz_sum = wz_sum * PI * wz_r * wz_r * wz_r / wz_n / wz_n;
wz_ans[3]=wz_sum;
pthread_exit(0);
}
inline double WallTime()
{ //calculate the time
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
double currTime = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
return currTime;
}
嫉泄安66
2015年10月21日 13:27
[bofu暴躁]