并行计算实例

【C++】计算从1~1E10的和

https://blog.csdn.net/chongkuachan8070/article/details/101041752

// paralle.cpp : 定义控制台应用程序的入口点。
//
 
 
#include "stdafx.h"
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
 
#define NUM_THREADS 4
 
int _tmain(int argc, _TCHAR* argv[])
{
	omp_set_num_threads(NUM_THREADS);
 
	long long sum = 0;
	long long sumtmp[NUM_THREADS];
	clock_t t1 = clock();
 
#pragma omp parallel 
	{
		long i;
		long id = omp_get_thread_num();
		long long temp = 0L;
#pragma omp for
		for(i = 1; i <= 1000000000; i++)
		{
			temp+=i;
		}
		sumtmp[id] = temp;
	}
 
	for(long i = 0; i < NUM_THREADS; i++)
	{
		sum += sumtmp[i];
	}
 
	clock_t t2 = clock();
	printf("sum=%lld\n", sum);
	printf("parallel time = %d\n", (t2-t1));
 
	sum = 0;
	t1 = clock();
	for(long i = 1; i <= 1000000000; i++)
		sum += i;
 
	t2 = clock();
	printf("sum=%lld\n", sum);
	printf("serial time = %d\n", (t2-t1));
 
	system("pause");
	return 0;
}

【Python】简单实现

https://blog.csdn.net/noirblack/article/details/79068068

from multiprocessing import Pool
from functools import partial 

def somefunc(str_1, str_2, iterable_iterm):
    print("%s %s %d" % (str_1, str_2, iterable_iterm))

def main():
    iterable = [1, 2, 3, 4, 5]
    pool = Pool()
    str_1 = "This"
    str_2 = "is"
    func = partial(somefunc, str_1, str_2)
    pool.map(func, iterable)
    pool.close()
    pool.join()

if __name__ == "__main__":
    main()

Quin_Meow

阿巴阿巴阿巴

相关推荐

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

微信扫一扫,分享到朋友圈

并行计算实例
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close