簡單的OpenMp程序步驟及代碼

1.點擊項目->屬性

在OpenMp支持那裏,寫上 是(/openmp)

注意:(/openmp)裏面的o和m必須是小寫的!

2.程序代碼

注意:如果寫#include "stdafx.h"時,報錯:無法找到預編譯文件stdafx.h那就直接將這句話去掉即可!

#include<Windows.h>
#include<tchar.h>
#include<stdio.h>
#include "omp.h"

int _tmain(int argc, _TCHAR *argv[]) {
	printf("Hello from serial. Thread number = % d\n", omp_get_thread_num());
#pragma omp parallel num_threads(4)
	{
		printf("Hello from parallel. Thread number = %d\n", omp_get_thread_num());
	}
	printf("Hello from serial again\n");
	system("pause");
	return 0;
}

運行結果:

這裏如果能運行出四個不同的Thread number就說明有四個線程在並行工作,並且每次的編譯結果都是不同的

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章