Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 具有两个嵌套for崩溃循环的OpenMP in-ray跟踪程序_Multithreading_C++11_Visual Studio 2015_Openmp - Fatal编程技术网

Multithreading 具有两个嵌套for崩溃循环的OpenMP in-ray跟踪程序

Multithreading 具有两个嵌套for崩溃循环的OpenMP in-ray跟踪程序,multithreading,c++11,visual-studio-2015,openmp,Multithreading,C++11,Visual Studio 2015,Openmp,我在VisualStudio 2015中使用OpenMP。 在我的光线跟踪算法中,我有以下代码 int x, y; #pragma omp parallel for private(y) for (x = 0; x < sWidth; x++) { for (y = 0; y < sHeight; y++) { thisone = y*sWidth + x; for (int aax = 0; aax < aadepth; aax++

我在VisualStudio 2015中使用OpenMP。 在我的光线跟踪算法中,我有以下代码

int x, y;
#pragma omp parallel for private(y)
for (x = 0; x < sWidth; x++) {
    for (y = 0; y < sHeight; y++) {

        thisone = y*sWidth + x;

        for (int aax = 0; aax < aadepth; aax++) {
            for (int aay = 0; aay < aadepth; aay++) {

                aa_index = aay*aadepth + aax;

                srand(time(0));

                //anti-aliasing
                if (sWidth > sHeight) {
                    //the image is wider than it is tall
                    xamnt = ((x + (double)aax / (aadepth - 1)) / sWidth)*aspectratio - (((sWidth - sHeight) / sHeight) / 2);
                    yamnt = ((sHeight - y) + (double)aax / (aadepth - 1)) / sHeight;
                }
                else if (sHeight > sWidth) {
                    //the image is taller than it is wide
                    xamnt = (x + (double)aax / (aadepth - 1)) / sWidth;
                    yamnt = (((sHeight - y) + (double)aax / (aadepth - 1)) / sHeight) / aspectratio - (((sHeight - sWidth) / sWidth) / 2);
                }
                else {
                    //the image is square
                    xamnt = (x + (double)aax / (aadepth - 1)) / sWidth;
                    yamnt = ((sHeight - y) + (double)aax / (aadepth - 1)) / sHeight;
                }


                //camera ray
                Vect cam_ray_origin = scene_cam.getCameraPosition();
                Vect cam_ray_direction = camdir.vectAdd(camright.vectMult(xamnt - 0.5).vectAdd(camdown.vectMult(yamnt - 0.5))).normalize();

                Ray cam_ray(cam_ray_origin, cam_ray_direction);

                vector<double> intersections;

                for (int index = 0; index < scene_objects.size(); index++) {
                    intersections.push_back(scene_objects.at(index)->findIntersection(cam_ray));
                }

                int index_of_winning_object = winningObjectIndex(intersections);

                if (index_of_winning_object == -1) {
                    //set the background black
                    tempRed[aa_index] = 0;
                    //cout << tempRed[aa_index];
                    tempGreen[aa_index] = 0;
                    tempBlue[aa_index] = 0;
                    //return color
                }
                else {
                    //index corresponds to an object in our scene
                    if (intersections.at(index_of_winning_object) > accuracy) {
                        //determine the position and direction vectors at the point of intersection

                        Vect intersection_position = cam_ray_origin.vectAdd(cam_ray_direction.vectMult(intersections.at(index_of_winning_object)));
                        Vect intersecting_ray_direction = cam_ray_direction;

                        //color in the points
                        Color intersection_color = getColorAt(intersection_position,
                            intersecting_ray_direction,
                            scene_objects, index_of_winning_object,
                            light_sources, accuracy, ambientlight);

                        tempRed[aa_index] = intersection_color.getColorRed();
                        tempGreen[aa_index] = intersection_color.getColorGreen();
                        tempBlue[aa_index] = intersection_color.getColorBlue();
                    }
                }
            }
        }
        //adding the colors to each pixel
        //average the pixel color
        double totalRed = 0;
        double totalGreen = 0;
        double totalBlue = 0;

        for (int iRed = 0; iRed < aadepth*aadepth; iRed++) {
            totalRed = totalRed + tempRed[iRed];
        }

        for (int iGreen = 0; iGreen < aadepth*aadepth; iGreen++) {
            totalGreen = totalGreen + tempGreen[iGreen];
        }

        for (int iBlue = 0; iBlue < aadepth*aadepth; iBlue++) {
            totalBlue = totalBlue + tempBlue[iBlue];
        }

        double avgRed = totalRed / (aadepth*aadepth);
        double avgGreen = totalGreen / (aadepth*aadepth);
        double avgBlue = totalBlue / (aadepth*aadepth);

        pixels[thisone].r = avgRed;
        pixels[thisone].g = avgGreen;
        pixels[thisone].b = avgBlue;
    }
}
saveimg("scene_anti-aliased_thread_test1.bmp", pixels);
为什么我会犯这些错误

当我在
aax
aay
之前使用相同的
#pragma omp parallel
进行循环时,它会使图像渲染得更慢。 现在我猜,当我在第二个两个嵌套for循环中使用
#pragma omp parallel for
时,它只会计算代码几次,就是这样。它没有任何作用

但我真的不知道,我只是开始玩openMP

请提供帮助-我应该使用哪些OpenMP函数以及应该将它们放在哪里

谢谢


附言-看到的代码主要来自于在线教程,用于从头制作光线跟踪器。Caleb Piercy制作光线跟踪器的基本方法。

您有许多比赛条件,如
此项、aa_索引、xamnt、yamnt
。将这些设置为私有或在并行区域内声明它们。您可能还有其他问题…
srand(时间(0))可疑…srand(时间(0))只跟踪执行整个程序所需的时间。我会尽量配合比赛条件,请提供一个。还要注意,您的代码不会崩溃。相反,您的编译器在编译过程中会给您一条非常具体的错误消息。
sWidth
的类型是什么?它是一个带sHeight的double。我发现如果是int,我可以使用“#pragma omp parallel for”,但当我尝试将其转换为int时,图像会被压扁。你有很多竞争条件,比如
这个、aa#index、xamnt、yamnt
。将这些设置为私有或在并行区域内声明它们。您可能还有其他问题…
srand(时间(0))可疑…srand(时间(0))只跟踪执行整个程序所需的时间。我会尽量配合比赛条件,请提供一个。还要注意,您的代码不会崩溃。相反,您的编译器在编译过程中会给您一条非常具体的错误消息。
sWidth
的类型是什么?它是一个带sHeight的double。我发现如果是int,我可以使用“#pragma omp parallel for”,但当我尝试将其转换为int时,图像会被压扁。
Error   C1903   unable to recover from previous error(s); stopping compilation  Ray-Tracing e:\utility programs\vs - c++\ray-tracing\ray-tracing\main.cpp   258