Win32API、MFC、.NET并行求数值积分PI(星星笔记)

15 篇文章 0 订阅
14 篇文章 0 订阅

1.运用Win32API实现并行数值积分,程序如下:

// Win32APIpi.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <omp.h>
#include <math.h>
#include "time.h"

HANDLE finish[2];
double pitmp[2] ={0.0,0.0};
static long num_steps = 1000000000;
double step = 1.0 /(double) num_steps;

DWORD WINAPI ThreadOne(LPVOID param)
{
	double t = 0;
	for (int i = 0;i <=(num_steps/2);i++)
	{
		double x;
		x = (i + 0.5)*step;
		t+=4.0/(1.0+x*x);
	}
	pitmp[0] = t;
	SetEvent(finish[0]);
	return 0;
}
DWORD WINAPI ThreadTwo(LPVOID param)
{
	double t = 0;
	for (int i = (num_steps/2)+1;i <=num_steps;i++)
	{
		double x;
		x = (i + 0.5)*step;
		t+=4.0/(1.0+x*x);
	}
	pitmp[1] = t;
	SetEvent(finish[1]);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	double tmp = 0,pi;
	clock_t start,end;

	start = clock();
	finish[0] = CreateEvent(NULL,false,false,NULL);
	finish[1] = CreateEvent(NULL,false,false,NULL);
	HANDLE Thread1 = CreateThread(NULL,0,ThreadOne,NULL,0,NULL);
	HANDLE Thread2 = CreateThread(NULL,0,ThreadTwo,NULL,0,NULL);
	WaitForMultipleObjects(2,finish,true,INFINITE);

	for (int i = 0;i<2;i++)
	{
		tmp +=pitmp[i];
	}
	pi = step *tmp;
	end = clock();
	printf("PI=%.15f\n",pi);
	printf("paraller time=%d\n",end - start);

	start = clock();
	tmp = 0.0;
	for (int i = 0;i <=num_steps;i++)
	{
		double x;
		x = (i + 0.5)*step;
		tmp +=4.0/(1.0+x*x);
	}
	pi = tmp *step;
	end = clock();
    printf("PI=%.15f\n",pi);
	printf("serial time=%d\n",end - start);
	system("pause");
	return 0;
}
运行结果如下:

2.运用MFC实现并行数值积分,程序如下:

// MFCpI.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <afxmt.h>
#include <iostream>
#include <afxwin.h>
#include "time.h"

CEvent faxEvent1(false);
CEvent faxEvent2(false);
double pitmp[2] = {0.0,0.0};
static long num_steps = 1000000000;
double step = 1.0 /(double) num_steps;

UINT ThreadOne(LPVOID param)
{
	double t = 0;
	for (int i = 0;i <=(num_steps/2);i++)
	{
		double x;
		x = (i + 0.5)*step;
		t+=4.0/(1.0+x*x);
	}
	pitmp[0] = t;
	SetEvent(faxEvent1);
	return 0;
}
UINT ThreadTwo(LPVOID param)
{
	double t = 0;
	for (int i = (num_steps/2)+1;i <=num_steps;i++)
	{
		double x;
		x = (i + 0.5)*step;
		t+=4.0/(1.0+x*x);
	}
	pitmp[1] = t;
	SetEvent(faxEvent2);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	double sum = 0,pi;
	clock_t start,end;

	start = clock();
	AfxBeginThread(ThreadOne,NULL);
	AfxBeginThread(ThreadTwo,NULL);
	WaitForSingleObject(faxEvent1,INFINITE);
	WaitForSingleObject(faxEvent2,INFINITE);
	for (int i = 0;i<2;i++)
	{
		sum +=pitmp[i];
	}
	pi = step *sum;
	end = clock();
	printf("PI=%.15f\n",pi);
	printf("paraller time=%d\n",end - start);

	start = clock();
	sum = 0.0;
	for (int i = 0;i <=num_steps;i++)
	{
		double x;
		x = (i + 0.5)*step;
		sum +=4.0/(1.0+x*x);
	}
	pi = sum *step;
	end = clock();
	printf("PI=%.15f\n",pi);
	printf("serial time=%d\n",end - start);

	system("pause");
	return 0;
}
运行结果如下:

3.运用.NET实现并行数值积分,程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace NETparallelpi
{
    class Program
    {
        static void Main(string[] args)
        {
            double pi;
            Stopwatch stopwatch = new Stopwatch();
            Work work1 = new Work(1);
            ThreadStart thread1 = new ThreadStart(work1.pSumto);
            Thread newthread1 = new Thread(thread1);
            Work work2 = new Work(2);
            ThreadStart thread2 = new ThreadStart(work2.pSumto);
            Thread newthread2 = new Thread(thread2);

            stopwatch.Start();
            newthread1.Start();
            newthread2.Start();

            newthread1.Join();
            newthread2.Join();
            stopwatch.Stop();

            TimeSpan timeSpan = stopwatch.Elapsed;
            pi = work1.getSum() + work2.getSum();
            double milliseconds = timeSpan.TotalMilliseconds;
            Console.Write("parallel pi  = {0}\n", pi);
            Console.Write("parallel time =");
            Console.Write(milliseconds);

            stopwatch.Start();
            pi = work1.sumto();
            stopwatch.Stop();
            timeSpan = stopwatch.Elapsed;
            milliseconds = timeSpan.TotalMilliseconds;
            Console.Write("\nserial pi  = {0}\n", pi);
            Console.Write("serial time =");
            Console.Write(milliseconds);
            Console.Read();
        }
    }
    class Work
    {
        private long start;
        private double[] sum = new double[2];
        private static long num_steps = 100000000;
        private double step = 1.0 / (double)num_steps;
        public Work(long i)
        {
            this.start = i;
        }
        public void pSumto()
        {
            double t = 0;
            for (long i = start; i <= num_steps; i += 2)
            {
                double x;
                x = (i + 0.5) * step;
                t += 4.0 / (1.0 + x * x);
            }
            sum[0] = t;
        }
        public double sumto()
        {
            double sumto = 0;
            for (long i = start; i <= num_steps; i += 1)
            {
                double x;
                x = (i + 0.5) * step;
                sumto += 4.0 / (1.0 + x * x);
            }
            return sumto * step;
        }
        public double getSum()
        {
            return (sum[0]+sum[1]) * step;
        }
    }
}
运行结果如下:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值