gpt4 book ai didi

c++ - 并行 STXXL vector 初始化

转载 作者:行者123 更新时间:2023-11-30 05:37:20 24 4
gpt4 key购买 nike

以下最小示例说明了在并行初始化容器(使用 openMP)时 stxxl 的行为:

#include <omp.h>
#include <stdio.h>
#include <stxxl.h>

typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;

int main(int argc, char* argv[]) {
const unsigned long NUM = 8;
#pragma omp parallel num_threads(NUM)
{
VEC_T v;
printf("%d\t%p\n", omp_get_thread_num(), &v);
}
return EXIT_SUCCESS;
}

遇到任何一个

[STXXL-ERROR] File too large 

[SYSTEM-ERROR]Segmentation fault

如何在多线程中分配 stxxl 容器?

最佳答案

stxxl 容器的初始化不是线程安全的,因此需要对初始化容器的线程进行互斥。使用 openMP,这将如下所示:

#include <omp.h>
#include <stdio.h>
#include <stxxl.h>

typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;

int main(int argc, char* argv[]) {
const unsigned long NUM = 8;
#pragma omp parallel num_threads(NUM)
{
VEC_T* v;
#pragma omp critical
{
v = new VEC_T();
}
printf("%d\t%p\n", omp_get_thread_num(), &v);
delete v;
}
return EXIT_SUCCESS;
}

关于c++ - 并行 STXXL vector 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33285327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com