将多维向量传递给C ++ 11线程

时间:2015-01-10 00:28:43

标签: c++ c++11 stdvector

我正在开发一个程序,该程序将一个充满无符号字符的大型二进制文件(> 80GB)读入std :: vector,然后在该向量中的元素块上运行计算。我想通过让独立的线程同时在向量的不同部分运行这些计算来加速,并在最后结合结果。

这些线程都应该将它们的计算结果存储在一对2维std :: vector中,我试图将它作为引用传递给线程函数调用。我可以将这一切都用于通用函数调用,但是当我尝试使用std :: thread启动它时,我得到以下编译错误:

In file included from /usr/include/c++/4.8/thread:39:0,
                 from test.cpp:12:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<int (*(long long unsigned int, long long unsigned int, std::vector<unsigned char>, std::vector<std::vector<long double> >, std::vector<std::vector<long long unsigned int> >))(long long unsigned int, long long unsigned int, std::vector<unsigned char>&, std::vector<std::vector<long double> >&, std::vector<std::vector<long long unsigned int> >&)>’:
/usr/include/c++/4.8/thread:137:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (&)(long long unsigned int, long long unsigned int, std::vector<unsigned char>&, std::vector<std::vector<long double> >&, std::vector<std::vector<long long unsigned int> >&); _Args = {long long unsigned int&, long long unsigned int&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<std::vector<long double, std::allocator<long double> >, std::allocator<std::vector<long double, std::allocator<long double> > > >&, std::vector<std::vector<long long unsigned int, std::allocator<long long unsigned int> >, std::allocator<std::vector<long long unsigned int, std::allocator<long long unsigned int> > > >&}]’
test.cpp:44:130:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<int (*(long long unsigned int, long long unsigned int, std::vector<unsigned char>, std::vector<std::vector<long double> >, std::vector<std::vector<long long unsigned int> >))(long long unsigned int, long long unsigned int, std::vector<unsigned char>&, std::vector<std::vector<long double> >&, std::vector<std::vector<long long unsigned int> >&)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<int (*(long long unsigned int, long long unsigned int, std::vector<unsigned char>, std::vector<std::vector<long double> >, std::vector<std::vector<long long unsigned int> >))(long long unsigned int, long long unsigned int, std::vector<unsigned char>&, std::vector<std::vector<long double> >&, std::vector<std::vector<long long unsigned int> >&)>’
         _M_invoke(_Index_tuple<_Indices...>)
     ^

我使用g ++ 4.8.2在Linux中编译:

g++ -Wall test.cpp -o test -std=c++0x 

非常感谢任何反馈。非常感谢!

我的代码如下:

#include <iostream>
#include <vector>
#include <fstream>
#include <thread>
#include <cstdlib>
#include <cmath>


int functionToRun (unsigned long long startingNum, unsigned long long endingNum, std::vector<unsigned char>& mainReadCountVector, std::vector<std::vector<long double>>& threadPWP, std::vector<std::vector<unsigned long long int>>& threadWeightings);

int main(int argc, const char * argv[])
{
    int numThreads = 32;

    std::streampos size;
    std::ifstream file ("data.bin", std::ios::in|std::ios::binary|std::ios::ate);
    if (file.is_open()) {
        // Create and populate the vector that holds all the data in the binary file
        size = file.tellg();
        file.seekg (0, std::ios::beg);
        std::vector<unsigned char> data(size);
        file.read((char*) &data[0], size);
        file.close();

        // The resulting calculations within each thread will be stored in a 2D vector that looks like std::vector<std::vector<long double>> calc1(272, std::vector<long double>(272,0)) and std::vector<std::vector<unsigned long long int>> calc2(272, std::vector<unsigned long long int>(272,0))
        // Create one of these vectors for each of the threads by making a 3D vector:
        std::vector<std::vector<std::vector<long double>>> calc1(numThreads, std::vector<std::vector<long double>> (272, std::vector<long double> (272,0) ) ); //calc1[0] is the first 2D array for the first thread, etc...
        std::vector<std::vector<std::vector<unsigned long long int>>> calc2(numThreads, std::vector<std::vector<unsigned long long int> > (272, std::vector<unsigned long long int> (272,0) ) );

        //Launch a group of threads
        std::vector<std::thread> threadsVec;
        for (int threadRunning = 0; threadRunning < numThreads; threadRunning++) {
            unsigned long long firstNum = threadRunning * 1000000;
            unsigned long long secondNum = (threadRunning * 1000000) + 1000000 - 1;
            threadsVec.push_back(std::thread(functionToRun, firstNum, secondNum, data, calc1[threadRunning], calc2[threadRunning]));
        }

        // Wait on threads to finish
        for (int i = 0; i < numThreads; ++i) {
            threadsVec[i].join();
        }

        // Now aggregate the results of the threads
        std::vector<std::vector<long double>> calc1Sum(272, std::vector<long double>(272,0));
        std::vector<std::vector<long double>> calc2Sum(272, std::vector<long double>(272,0));
        for (int element = 0; element < 272; element++) {
            for (int comparisonElement = 0; comparisonElement <= element; comparisonElement++) {
                for (int threadVector = 0; threadVector <= numThreads; threadVector++) {
                    calc1Sum[element][comparisonElement] += calc1[threadVector][element][comparisonElement];
                    calc2Sum[element][comparisonElement] += calc2[threadVector][element][comparisonElement];
                }
            }
        }
    } else std::cout << "Unable to open data.bin";

    return 0;
}


int functionToRun (unsigned long long startingNum, unsigned long long endingNum, std::vector<unsigned char>& mainDataVector, std::vector<std::vector<long double>>& calc1ret, std::vector<std::vector<unsigned long long int>>& calc2ret) {

    int internalData[272];

    for (unsigned long long dataSpot = startingNum; dataSpot < endingNum; dataSpot++) {
        for (int ind1 = 0; ind1 < 272; ind1++) {
            unsigned long long firstIndex = dataSpot * 544 + 2 * ind1;
            unsigned long long secondIndex = dataSpot * 544 + 2 * ind1 + 1;
            internalData[ind1] = mainDataVector[firstIndex] + mainDataVector[secondIndex];

            for (int ind2 = 0; ind2 <= ind1; ind2++) {
                calc1ret[ind1][ind2] = internalData[ind1] + internalData[ind2];
                calc2ret[ind1][ind2] = internalData[ind1] / (std::pow(internalData[ind2],2));
            }
        }
    }
    return 0;
}

0 个答案:

没有答案