Thread
- 设置多线程数上限:
HmppResult HMPP_SetNumberThreads (int32_t numberThreads);
- 获取当前的线程数:
HmppResult HMPP_GetNumberThreads (int32_t* numberThreads);
- 获取当前线程ID:
HmppResult HMPP_GetThreadIdx (int32_t* threadIdx);
- 获取当前多线程模式:
HmppResult HMPP_GetThreadType (HmppThreadType* threadType);
参数
参数名 |
描述 |
取值范围 |
输入/输出 |
---|---|---|---|
numberThreads |
要限定的线程数上限(SetNumberThreads)。 |
大于0 |
输入 |
numberThreads |
目标地址,指向内存存放当前线程数(GetNumberThreads)。 |
非空 |
输出 |
threadIdx |
目标地址,指向内存存放当前线程的ID。 |
非空 |
输出 |
threadType |
目标地址,指向内存存放当前线程模式。 |
非空 |
输出 |
返回值
- 成功:返回HMPP_STS_NO_ERR。
- 失败:返回错误码。
错误码
错误码 |
描述 |
---|---|
HMPP_STS_NULL_PTR_ERR |
传入指针是空指针。 |
HMPP_STS_SIZE_ERR |
len小于或等于0。 |
示例
#define NUMBER_THREAD_LIM 4 void Thread_Example() { HmppResult result = HMPP_SetNumberThreads(NUMBER_THREAD_LIM); printf("%s\n", HMPP_GetStatusString(result)); HmppThreadType testThrType; result = HMPP_GetThreadType(&testThrType); printf("%s\n", HMPP_GetStatusString(result)); if (testThrType == OMP) { printf("thread mode is omp\n"); #pragma omp parallel for for (int32_t i = 0; i < NUMBER_THREAD_LIM; ++i) { int32_t testNumThr, testThrIdx; HMPP_GetNumberThreads(&testNumThr); HMPP_GetThreadIdx(&testThrIdx); printf("testNumThr = %d, testThrIdx = %d\n", testNumThr, testThrIdx); } } else { printf("no thread mode\n"); int32_t testNumThr, testThrIdx; HMPP_GetNumberThreads(&testNumThr); HMPP_GetThreadIdx(&testThrIdx); printf("testNumThr = %d, testThrIdx = %d\n", testNumThr, testThrIdx); } }
运行结果:
- 第一种:HMPP库没有开启多线程模式。
No Error No Error no thread mode testNumThr = 1, testThrIdx = 0
- 第二种:HMPP库开启多线程模式。
No Error No Error thread mode is omp testNumThr = 4, testThrIdx = 1 testNumThr = 4, testThrIdx = 3 testNumThr = 4, testThrIdx = 2 testNumThr = 4, testThrIdx = 0
- 目前HMPP仅发布二进制包,且未开启OMP编译选项,因此暂不支持多线程的功能。
- 若支持多线程功能,几个线程之间的打印顺序可能与上述示例回显结果不同。
父主题: 基础函数