多核编程与并行计算实验报告 (1)
更新时间:2023-06-06 07:39:01 阅读量: 实用文档 文档下载
多核编程与并行计算
多核编程与并行计算实验报告
姓名:
日期:2014年 4月20日
实验一
// exa1.cpp : Defines the entry point for the console application.
多核编程与并行计算
//
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <iostream>
#include <fstream>
using namespace std;
void ThreadFunc1(PVOID param)
{
}
void ThreadFunc2(PVOID param)
{
}
int main()
{
}
int i=0; _beginthread(ThreadFunc1,0,NULL); _beginthread(ThreadFunc2,0,NULL); Sleep(3000); cout<<"end"<<endl; return 0; while(1) { } Sleep(1000); cout<<"This is ThreadFunc2"<<endl; while(1) { } Sleep(1000); cout<<"This is ThreadFunc1"<<endl;
多核编程与并行计算
实验二
// exa2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI FunOne(LPVOID param){
while(true)
{
Sleep(1000);
cout<<"hello! ";
}
return 0;
}
DWORD WINAPI FunTwo(LPVOID param){
while(true)
{
Sleep(1000);
cout<<"world! ";
多核编程与并行计算
}
return 0;
}
int main(int argc, char* argv[])
{
int input=0;
HANDLE hand1=CreateThread (NULL, 0, FunOne, (void*)&input, CREATE_SUSPENDED, NULL); HANDLE hand2=CreateThread (NULL, 0, FunTwo, (void*)&input, CREATE_SUSPENDED, NULL); while(true){
cin>>input;
if(input==1)
{
ResumeThread(hand1); ResumeThread(hand2);
}
else
{
SuspendThread(hand1); SuspendThread(hand2);
}
};
TerminateThread(hand1,1);
TerminateThread(hand2,1);
return 0;
}
多核编程与并行计算
实验三
// exa3.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int globalvar = false;
DWORD WINAPI ThreadFunc(LPVOID pParam)
{
}
int main()
{
}
HANDLE hthread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL); if (!hthread) { } while (!globalvar) cout<<"Thread while"<<endl; cout<<"Thread exit"<<endl; return 0; cout<<"Thread Create Error ! "<<endl; CloseHandle(hthread); cout<<"ThreadFunc"<<endl; Sleep(200); globalvar = true; return 0;
多核编程与并行计算
实验四:
// exa4.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <iostream>
#include <fstream>
using namespace std;
HANDLE evRead, evFinish;
void ReadThread(LPVOID param)
{
}
void WriteThread(LPVOID param)
{
}
int main(int argc , char * argv[])
{ cout<<"Writing"<<endl; SetEvent (evRead); WaitForSingleObject (evRead ,INFINITE); cout<<"Reading"<<endl; SetEvent (evFinish);
多核编程与并行计算
}
evFinish = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ; _beginthread(ReadThread , 0 , NULL) ; _beginthread(WriteThread , 0 , NULL) ; WaitForSingleObject (evFinish,INFINITE) ; cout<<"The Program is End"<<endl; return 0 ;
实验五
// exa5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <iostream>
#include <fstream>
using namespace std;
int total = 100 ;
HANDLE evFin[2] ;
CRITICAL_SECTION cs ;
void WithdrawThread1(LPVOID param)
多核编程与并行计算
}
void WithdrawThread2(LPVOID param)
{
}
int main(int argc , char * argv[])
{
}
evFin[0] = CreateEvent (NULL,FALSE,FALSE,NULL) ; evFin[1] = CreateEvent (NULL,FALSE,FALSE,NULL) ; InitializeCriticalSection(&cs) ; _beginthread(WithdrawThread1 , 0 , NULL) ; _beginthread(WithdrawThread2 , 0 , NULL) ; WaitForMultipleObjects(2 ,evFin ,TRUE ,INFINITE) ; DeleteCriticalSection(&cs) ; cout<<total<<endl; return 0 ; EnterCriticalSection(&cs) ; if ( total-20 >= 0) { } else cout<<"You do not have that much money"<<endl; LeaveCriticalSection(&cs) ; LeaveCriticalSection(&cs) ; SetEvent (evFin[1]) ; total -= 20 ; cout<<"You withdraw 20"<<endl; EnterCriticalSection(&cs) ; if ( total-90 >= 0) { } else cout<<"You do not have that much money"<<endl; LeaveCriticalSection(&cs) ; SetEvent (evFin[0]) ; total -= 90 ; cout<<"You withdraw 90"<<endl;
多核编程与并行计算
实验六:
// exa6.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
#define THREAD_INSTANCE_NUMBER 3
LONG g_fResourceInUse = FALSE;
LONG g_lCounter = 0;
DWORD ThreadProc(void * pData) {
cout << "ThreadProc: " << ThreadNumberTemp << " is running!" << endl; if ((hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Mutex.Test")) == NULL) { } cout << "ThreadProc " << ThreadNumberTemp << " gets the mutex"<< endl; ReleaseMutex(hMutex); cout << "Open Mutex error!" << endl; int ThreadNumberTemp = (*(int*) pData); HANDLE hMutex;
多核编程与并行计算
}
CloseHandle(hMutex); return 0;
int main(int argc, char* argv[])
{
}
return 0; WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE); cout << "Close the Mutex Handle! " << endl; CloseHandle(hMutex); } cout << "CreateThread error" << ID[i] << endl; cout << "CreateThread: " << ID[i] << endl; else h[i] = CreateThread(NULL, if (h[i] == NULL) 0, (LPTHREAD_START_ROUTINE) ThreadProc, (void *)&ID[i], 0, &(ID[i])); for (i=0;i<THREAD_INSTANCE_NUMBER;i++) { int i; DWORD ID[THREAD_INSTANCE_NUMBER]; HANDLE hMutex; if ( (hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Mutex.Test")) == NULL) { } if ((hMutex = CreateMutex(NULL, FALSE, "Mutex.Test")) == NULL ) { } cout << "Create Mutex error!" << endl; return 0; HANDLE h[THREAD_INSTANCE_NUMBER];
多核编程与并行计算
实验七
// exa7.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
#define THREAD_INSTANCE_NUMBER 3
DWORD foo(void * pData) {
}
int main(int argc, char* argv[])
{
cout << "foo: " << ThreadNumberTemp << " is running!" << endl; if ((hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, "Semaphore.Test")) == } cout << "foo " << ThreadNumberTemp << " gets the semaphore"<< endl; ReleaseSemaphore(hSemaphore, 1, NULL); CloseHandle(hSemaphore); return 0; cout << "Open Semaphore error!" << endl; int ThreadNumberTemp = (*(int*) pData); HANDLE hSemaphore; NULL) {
多核编程与并行计算
}
int i; DWORD ThreadID[THREAD_INSTANCE_NUMBER]; HANDLE hSemaphore; if ((hSemaphore = CreateSemaphore(NULL,0,1, "Semaphore.Test")) == NULL ) { } cout << "Create Semaphore error!" << endl; return 0; HANDLE hThread[THREAD_INSTANCE_NUMBER]; for (i=0;i<THREAD_INSTANCE_NUMBER;i++) { } WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE); cout << "Close the Semaphore Handle! " << endl; CloseHandle(hSemaphore); return 0; hThread[i] = CreateThread(NULL, if (hThread[i] == NULL) cout << "CreateThread error" << ThreadID[i] << endl; cout << "CreateThread: " << ThreadID[i] << endl; 0, (LPTHREAD_START_ROUTINE) foo, (void *)&ThreadID[i], 0, &(ThreadID[i])); else
多核编程与并行计算
实验八:
// exa8.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "exa8.h"
#include "MainFrm.h"
#include "exa8Doc.h"
#include "exa8View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CExa8App
BEGIN_MESSAGE_MAP(CExa8App, CWinApp)
//{{AFX_MSG_MAP(CExa8App) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands
多核编程与并行计算
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CExa8App construction
CExa8App::CExa8App()
{
}
///////////////////////////////////////////////////////////////////////////// // The one and only CExa8App object
CExa8App theApp;
///////////////////////////////////////////////////////////////////////////// // CExa8App initialization
BOOL CExa8App::InitInstance()
{
#ifdef _AFXDLL
// Register the application's document templates. Document templates LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. AfxEnableControlContainer(); // TODO: add construction code here, // Place all significant initialization in InitInstance
多核编程与并行计算
}
CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CExa8Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CExa8View)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE;
///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
// Dialog Data
// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA CAboutDlg();
多核编程与并行计算
// Implementation
protected:
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
// App command to run the dialog
void CExa8App::OnAppAbout()
{
}
///////////////////////////////////////////////////////////////////////////// // CExa8App message handlers
CAboutDlg aboutDlg; aboutDlg.DoModal(); //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() END_MESSAGE_MAP()
多核编程与并行计算
、实验九:
using System;
using System.Threading;
class Test
{
}
class Work
{
public static void DoWork() { } public int Data; public void DoMoreWork() { Console.WriteLine("Instance thread procedure. Data={0}", Data); Console.WriteLine("Static thread procedure."); static void Main() { } ThreadStart threadDelegate = new ThreadStart(Work.DoWork); Thread newThread = new Thread(threadDelegate); newThread.Start(); Work w = new Work(); w.Data = 42; threadDelegate = new ThreadStart(w.DoMoreWork); newThread = new Thread(threadDelegate); newThread.Start();
多核编程与并行计算
}
实验十:
using System;
using System.Threading;
class Test
{
} public static void WithDraw2() static int total = 100; public static void WithDraw1() { int n=90; if (n <= total) { } else { } Console.WriteLine("You do not enough money. n={0}", n); Console.WriteLine("total={0}", total); total -= n; Console.WriteLine("You have withdrawn. n={0}", n); Console.WriteLine("total={0}", total);
多核编程与并行计算
}
} public static void Main() { } ThreadStart thread1 = new ThreadStart(WithDraw1); Thread newThread1 = new Thread(thread1); ThreadStart thread2 = new ThreadStart(WithDraw2); Thread newThread2 = new Thread(thread2); newThread1.Start(); newThread2.Start(); int n = 20; if (n <= total) { } else { } Console.WriteLine("You do not enough money. n={0}", n); Console.WriteLine("total={0}", total); total -= n; Console.WriteLine("You have withdrawn. n={0}", n); Console.WriteLine("total={0}", total);
多核编程与并行计算
实验十一:
// exa11.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#define THREAD_INSTANCE_NUMBER 3
LONG g_fResourceInUse = FALSE;
LONG g_lCounter = 0;
CRITICAL_SECTION cs;
DWORD ThreadProc1(void * pData) {
return 0; EnterCriticalSection(&cs); printf("ThreadProc1 %d enters into critical section\n",ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); int ThreadNumberTemp = (*(int*) pData); printf("ThreadProc1: %d is running!\n",ThreadNumberTemp );
多核编程与并行计算
DWORD ThreadProc2(void * pData) {
}
int main(int argc, char* argv[]) {
h2= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc2, (void *)&ID2, 0, &(ID2)); int i; DWORD ID1,ID2; InitializeCriticalSection(&cs); printf("Create the critical section \n"); h1 = CreateThread(NULL, printf("CreateThread error %d \n",ID1); printf("CreateThread %d \n",ID1); 0, (LPTHREAD_START_ROUTINE) ThreadProc1, (void *)&ID1, 0, &(ID1)); return 0; EnterCriticalSection(&cs); printf("ThreadProc2 %d enters into critical section\n",ThreadNumberTemp); Sleep(1000); LeaveCriticalSection(&cs); int ThreadNumberTemp = (*(int*) pData); printf("ThreadProc2: %d is running!\n",ThreadNumberTemp ); HANDLE h1,h2; if (h1 == NULL) else
正在阅读:
多核编程与并行计算实验报告 (1)06-06
中国古代史主线脉络图06-25
关于孝顺的小学作文06-15
2013-2014学年山西省左权县麻田中学八年级下学期期中考试英语试03-08
国家税务总局公告2012年第24号关于发布《出口货物劳务增值税和消06-02
浙江省2013年度4月高等教育自学考试 药物化学(二)试题 课程代码10116 11-26
2015国培研修心得体会04-22
叶子作文600字07-11
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 多核
- 并行
- 编程
- 实验
- 计算
- 报告
- 员工满意度实证研究
- 高数第二章复习题参考答案
- 2012年中国政府网站绩效评估指标体系(区县)
- yh药学110411 第十六章 基因工程及其在医学中的应用
- 【乱象社会】湖北省委巡视组下去20天,留下了许多令人思考的故事
- 上海农商银行金融便利店走进社区
- 2018-2019武汉市小学毕业数学总复习小升初模拟训练试卷8-9(共2套)附详细试题答案
- 2011年公共基础知识预测试卷及答案
- 欧碧玺情侣婚纱摄影会所异业合作
- 模拟卷湖北省黄冈中学2010届高三11月月考英语试题
- 第五章电气消防基础知识
- 第7章 西门子PLC通信技术
- 2011年零售业十大关键词
- 13.上海交通大学2011年世界大学排名
- 高中地理小论文例文
- 综合性学习与各地经典阅读资料
- 第6讲 立体几何问题的题型与方法——范例分析
- 人生若只如初见 何事秋风悲画扇什么意思
- 中国建筑设计研究院2012年研究生招生简章
- 07建筑工程计价模拟试卷(湖州)_secret