#include <stdio.h>
#include <pthread.h>
#include "kupl.h"
static inline void task_in_loop(kupl_nd_range_t *nd_range, void *args, int tid, int tnum)
{
setbuf(stdout, NULL);
printf("Thread %d: nd_range->dim = %d\n", tid, nd_range->dim);
printf("test\n");
printf("the nd_range->nd_range[0].upper is %zu, the nd_range->nd_range[0].lower is %zu\n",nd_range->nd_range[0].upper,nd_range->nd_range[0].lower);
printf("the nd_range->nd_range[1].upper is %zu, the nd_range->nd_range[1].lower is %zu\n",nd_range->nd_range[1].upper,nd_range->nd_range[1].lower);
for (int i = nd_range->nd_range[0].lower; i < nd_range->nd_range[0].upper; i += nd_range->nd_range[0].step) {
for (int j = nd_range->nd_range[1].lower; j < nd_range->nd_range[1].upper; j += nd_range->nd_range[1].step) {
printf("pthread %lu: task_in_loop exe\n", pthread_self());
}
}
}
int main()
{
int num_threads = kupl_get_num_executors();
printf("the num_threads is %d\n",num_threads);
// num_threads=2;
kupl_nd_range_t range;
KUPL_2D_RANGE_INIT(range, 0, num_threads, 1, 0, num_threads, 1);
printf("The dim is %zu\n",range.dim);
// 3. 【关键】在 Main 中检查宏是否生效
printf("\n--- Main Setup Check ---\n");
printf("Main Range Dim 0 Upper: %zu (Expected: %d), Low: %zu (Expected: %d)\n", range.nd_range[0].upper, num_threads, range.nd_range[0].lower, num_threads);
printf("Main Range Dim 1 Upper: %zu (Expected: %d), Low: %zu (Expected: %d)\n", range.nd_range[1].upper, num_threads, range.nd_range[1].lower, num_threads);
int executors[num_threads];
for (int i = 0; i < num_threads; i++) {
executors[i] = i;
}
kupl_egroup_h eg = kupl_egroup_create(executors, num_threads);
kupl_parallel_for_desc_t desc = {
.range = &range,
.concurrency = num_threads,
.egroup = eg,
.policy = KUPL_LOOP_POLICY_STATIC
};
kupl_parallel_for(&desc, task_in_loop, nullptr);
printf("parallel_for 返回,即将销毁 egroup\n");
fflush(stdout);
kupl_egroup_destroy(eg);
printf("程序正常结束\n");
fflush(stdout);
// kupl_egroup_destroy(eg);
}
测试kupl_parallel_for效果的时候发现
没有 task_in_loop函数中printf("pthread %lu: task_in_loop exe\n", pthread_self());的输出
查看发现task_in_loop中nd_range->nd_range[1]的upper和lower都是0
查看发现main中nd_range->nd_range[1]的upper和lower是正常的
再测试发现main中dim为2
printf("The dim is %zu\n",range.dim);
在task_in_loop中dim为1
printf("Thread %d: nd_range->dim = %d\n", tid, nd_range->dim);
结论是kupl强行自动降维
如何控制kupl的行为,不让他降维?
他这样降维导致代码运行异常,这是一个严重的逻辑BUG(自动降维)
#include <stdio.h>
#include <pthread.h>
#include "kupl.h"
static inline void task_in_loop(kupl_nd_range_t *nd_range, void *args, int tid, int tnum)
{
setbuf(stdout, NULL);
printf("Thread %d: nd_range->dim = %d\n", tid, nd_range->dim);
printf("test\n");
printf("the nd_range->nd_range[0].upper is %zu, the nd_range->nd_range[0].lower is %zu\n",nd_range->nd_range[0].upper,nd_range->nd_range[0].lower);
printf("the nd_range->nd_range[1].upper is %zu, the nd_range->nd_range[1].lower is %zu\n",nd_range->nd_range[1].upper,nd_range->nd_range[1].lower);
for (int i = nd_range->nd_range[0].lower; i < nd_range->nd_range[0].upper; i += nd_range->nd_range[0].step) {
for (int j = nd_range->nd_range[1].lower; j < nd_range->nd_range[1].upper; j += nd_range->nd_range[1].step) {
printf("pthread %lu: task_in_loop exe\n", pthread_self());
}
}
}
int main()
{
int num_threads = kupl_get_num_executors();
printf("the num_threads is %d\n",num_threads);
// num_threads=2;
kupl_nd_range_t range;
KUPL_2D_RANGE_INIT(range, 0, num_threads, 1, 0, num_threads, 1);
printf("The dim is %zu\n",range.dim);
// 3. 【关键】在 Main 中检查宏是否生效
printf("\n--- Main Setup Check ---\n");
printf("Main Range Dim 0 Upper: %zu (Expected: %d), Low: %zu (Expected: %d)\n", range.nd_range[0].upper, num_threads, range.nd_range[0].lower, num_threads);
printf("Main Range Dim 1 Upper: %zu (Expected: %d), Low: %zu (Expected: %d)\n", range.nd_range[1].upper, num_threads, range.nd_range[1].lower, num_threads);
int executors[num_threads];
for (int i = 0; i < num_threads; i++) {
executors[i] = i;
}
kupl_egroup_h eg = kupl_egroup_create(executors, num_threads);
kupl_parallel_for_desc_t desc = {
.range = &range,
.concurrency = num_threads,
.egroup = eg,
.policy = KUPL_LOOP_POLICY_STATIC
};
kupl_parallel_for(&desc, task_in_loop, nullptr);
printf("parallel_for 返回,即将销毁 egroup\n");
fflush(stdout);
kupl_egroup_destroy(eg);
printf("程序正常结束\n");
fflush(stdout);
// kupl_egroup_destroy(eg);
}
测试kupl_parallel_for效果的时候发现
没有 task_in_loop函数中printf("pthread %lu: task_in_loop exe\n", pthread_self());的输出
查看发现task_in_loop中nd_range->nd_range[1]的upper和lower都是0
查看发现main中nd_range->nd_range[1]的upper和lower是正常的
再测试发现main中dim为2
printf("The dim is %zu\n",range.dim);
在task_in_loop中dim为1
printf("Thread %d: nd_range->dim = %d\n", tid, nd_range->dim);
结论是kupl强行自动降维
如何控制kupl的行为,不让他降维?
他这样降维导致代码运行异常,这是一个严重的逻辑BUG(自动降维)