【问题标题】:C++ fstream outputs wrong dataC++ fstream 输出错误数据
【发布时间】:2011-11-11 17:29:48
【问题描述】:

上下文优先:

我的程序做了一些并行计算,这些计算记录在一个文件中。线程按块分组(我使用的是 CUDA)。日志文件格式如下:

#begin run
({blockIdx,threadIdx}) {thread_info}
({blockIdx,threadIdx}) {thread_info}
...
#end run

我编写了一个函数,它应该读取日志文件并按线程对每个运行消息进行排序。

//------------------------------------------------------------------------------
// Comparison struct for log file sorting
//------------------------------------------------------------------------------
typedef struct
{
    bool operator()(const string &rString1 , const string &rString2)
    {
        int closeParenthesisLocalition1 = rString1.find_first_of(')');
        int closeParenthesisLocalition2 = rString2.find_first_of(')');
        int compResult = rString1.compare(0 , closeParenthesisLocalition1 + 2 , rString2 , 0 , closeParenthesisLocalition2 + 2);
        return (compResult < 0);
    }
} comp;

//------------------------------------------------------------------------------------
// Sort the log file. Lines with same prefix (blockIdx,ThreadIdx) will be grouped in file per run.
//------------------------------------------------------------------------------------
void CudaUnitTest::sortFile()
{
    comp comparison;
    deque<string> threadsPrintfs;
    ifstream inputFile(m_strInputFile);
    assert(inputFile.is_open());

    //Read whole input file and close it. Saves disk accesses.
    string strContent((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
    inputFile.close();

    ofstream outputFile(m_strOutputFile);
    assert(outputFile.is_open());

    string strLine;
    int iBeginRunIdx = -10; //value just to addapt on while loop (to start on [0])
    int iBeginRunNewLineOffset = 10; //"idx offset to a new line char in string. Starts with the offset of the string "#begin run\n".
    int iEndRunIdx;
    int iLastNewLineIdx;
    int iNewLineIdx;

    while((iBeginRunIdx = strContent.find("#begin run\n" , iBeginRunIdx + iBeginRunNewLineOffset)) != string::npos)
    {
        iEndRunIdx = strContent.find("#end run\n" , iBeginRunIdx + iBeginRunNewLineOffset);
        assert(iEndRunIdx != string::npos);

        iLastNewLineIdx = iBeginRunIdx + iBeginRunNewLineOffset;
        while((iNewLineIdx = strContent.find("\n" , iLastNewLineIdx + 1)) < iEndRunIdx)
        {
            strLine = strContent.substr(iLastNewLineIdx + 1 , iNewLineIdx);
            if(verifyPrefix(strLine))
                threadsPrintfs.push_back(strLine);
            iLastNewLineIdx = iNewLineIdx;
        }

        //sort last run info
        sort(threadsPrintfs.begin() , threadsPrintfs.end() , comparison);
        threadsPrintfs.push_front("#begin run\n");
        threadsPrintfs.push_back("#end run\n");

        //output it
        for(deque<string>::iterator it = threadsPrintfs.begin() ; it != threadsPrintfs.end() ; ++it)
        {
            assert(outputFile.good());
            outputFile.write(it->c_str() , it->size());
        }
        outputFile.flush();
        threadsPrintfs.clear();
    }

    outputFile.close();
}

问题是生成的文件有很多垃圾数据。例如,一个 6KB 的输入日志文件生成了一个 192KB 的输出日志!输出文件似乎与输入文件有很多重复。但是,在调试代码时,双端队列在排序前后显示了正确的值。我认为ofstream写入本身有问题。

编辑:该函数没有并行运行。

【问题讨论】:

  • 是多个线程同时写入吗?
  • 您是否应该保护您的 std::ofs。使用互斥锁进行调用?
  • @Batchyx:我看到了,我喜欢你在那里做的。
  • 这段代码是经过并行计算的。它的序列号。
  • substr 接受长度,而不是结束索引。你为什么在双端队列中有正确的字符串?一般来说,摆弄索引是一个非常糟糕的主意。使用普通的getline()。不要为保存文件访问而烦恼,这就是文件缓冲区的用途。 如果分析显示您在访问文件上浪费时间,则增加缓冲区大小并完成它。

标签: c++ parallel-processing ofstream


【解决方案1】:

只是为了显示最终代码。注意 substr 上的变化,现在它接收长度而不是索引。

//------------------------------------------------------------------------------------
// Sort the log file. Lines with same prefix (blockIdx,ThreadIdx) will be grouped in file per run.
//------------------------------------------------------------------------------------
void CudaUnitTest::sortFile()
{
comp comparison;
deque<string> threadsPrintfs;
ifstream inputFile(m_strInputFile);
assert(inputFile.is_open());

//Read whole input file and close it. Saves disk accesses.
string strContent((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
inputFile.close();

ofstream outputFile(m_strOutputFile);
assert(outputFile.is_open());

string strLine;
int iBeginRunIdx = -10; //value just to addapt on while loop (to start on [0])
int iBeginRunNewLineOffset = 10; //"idx offset to a new line char in string. Starts with the offset of the string "#begin run\n".
int iEndRunIdx;
int iLastNewLineIdx;
int iNewLineIdx;

while((iBeginRunIdx = strContent.find("#begin run\n" , iBeginRunIdx + iBeginRunNewLineOffset)) != string::npos)
{
    iEndRunIdx = strContent.find("#end run\n" , iBeginRunIdx + iBeginRunNewLineOffset);
    assert(iEndRunIdx != string::npos);

    iLastNewLineIdx = iBeginRunIdx + iBeginRunNewLineOffset;
    while((iNewLineIdx = strContent.find("\n" , iLastNewLineIdx + 1)) < iEndRunIdx)
    {
        strLine = strContent.substr(iLastNewLineIdx + 1 , iNewLineIdx - iLastNewLineIdx);
        if(verifyPrefix(strLine))
            threadsPrintfs.push_back(strLine);
        iLastNewLineIdx = iNewLineIdx;
    }

    //sort last run info
    sort(threadsPrintfs.begin() , threadsPrintfs.end() , comparison);
    threadsPrintfs.push_front("#begin run\n");
    threadsPrintfs.push_back("#end run\n");

    //output it
    for(deque<string>::iterator it = threadsPrintfs.begin() ; it != threadsPrintfs.end() ; ++it)
    {
        assert(outputFile.good());
        outputFile.write(it->c_str() , it->size());
    }
    threadsPrintfs.clear();
}

outputFile.close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多