首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过图形处理器读取多个.dat文件

通过图形处理器读取多个.dat文件
EN

Stack Overflow用户
提问于 2013-04-23 10:49:51
回答 1查看 747关注 0票数 2

我知道使用GPU读取文件是一项低效的任务,因为它面临着系统中最慢的部分,即IO。然而,我想出了另一种方法,使用CPU读取文件,并让GPU来处理处理负担。我用C++写了下面的代码,但我被困在了集成点,也就是说,在这些文件被中央处理器读取后,如何让图形处理器处理这些文件。换句话说,C++-amp添加并与代码集成的起点是什么?或者我应该从头开始重写整个代码?

代码语言:javascript
复制
{/* this code to read multiple .dat files from the directory that contains the implementation (from my account of stackoverflow) */

#include <Windows.h>
#include <ctime>
#include <stdint.h>
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;

#include <cstring>

/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
 * windows and linux. */

uint64_t GetTimeMs64()
{


 FILETIME ft;
 LARGE_INTEGER li;

 /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  * to a LARGE_INTEGER structure. */
 GetSystemTimeAsFileTime(&ft);
 li.LowPart = ft.dwLowDateTime;
 li.HighPart = ft.dwHighDateTime;

 uint64_t ret;
 ret = li.QuadPart;
 ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
 ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */

 return ret;

}


const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = "|";

int main()
{
  // create a file-reading object
  uint64_t a = GetTimeMs64();
  cout << a << endl;
  HANDLE h;
WIN32_FIND_DATA find_data;
h = FindFirstFile( "*.dat", & find_data );
if( h == INVALID_HANDLE_VALUE ) {
    cout<<"error"<<endl;

}
do {
    char * s = find_data.cFileName;

    ifstream fin;
  fin.open(s); // open a file
  if (!fin.good()) 
    return 1; // exit if file not found

  // read each line of the file
  while (!fin.eof())
  {
    // read an entire line into memory
    char buf[MAX_CHARS_PER_LINE];
    fin.getline(buf, MAX_CHARS_PER_LINE);

    // parse the line into blank-delimited tokens
    int n = 0; // a for-loop index

    // array to store memory addresses of the tokens in buf
    const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0

    // parse the line
    token[0] = strtok(buf, DELIMITER); // first token
    if (token[0]) // zero if line is blank
    {
      for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
      {
    token[n] = strtok(0, DELIMITER); // subsequent tokens
        if (!token[n]) break; // no more tokens
  }
}

    // process (print) the tokens
    for (int i = 0; i < n; i++) // n = #of tokens
      cout << "Token[" << i << "] = " << token[i] << endl;
    cout << endl;
  }
            // Your code here
} while( FindNextFile( h, & find_data ) );
FindClose( h );



  uint64_t b = GetTimeMs64();
  cout << a << endl;
  cout << b << endl;
  uint64_t c = b - a;
  cout << c << endl;

  system("pause");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-23 15:57:39

没有办法处理GPU的文件。正如您假设的那样,CPU处理IO。因此,您需要将读取的信息存储在内存中,将其发送到GPU,在那里进行计算等。

处理文件的一个好方法是存档(使用GPU)您的信息。

所以你用CPU读取文件,用GPU解压>计算>存档,然后用CPU存储它。

UPD。

代码语言:javascript
复制
(CPU IO READ from file (should be already archived information))  to -> main memory
(CPU SEND) to -> GPU global memory from main memory

(GPU EXTRACT (if archived))
(GPU COMPUTE (your work here))
(GPU ARCHIVE)

(CPU RETRIEVE) to -> main memory from GPU global memory
(CPU IO WRITE to file)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16160073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档