|
| 1 | +// |
| 2 | +// Created by 梁俊鹏 on 2019-07-20. |
| 3 | +// |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | +#include "read_io.h" |
| 9 | +#include "entity/read.h" |
| 10 | + |
| 11 | +#define BUFFER_SIZE 152 |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +//block_queue<string> *ioqueue = new block_queue<string>; |
| 16 | + |
| 17 | +static int thisRank; |
| 18 | +static int host_num; |
| 19 | +static string path; |
| 20 | +static string *fnames; |
| 21 | +static int fileAmount = 0; |
| 22 | +static int curIndex4Fnames; |
| 23 | +static fpos_t curSeek; |
| 24 | +static char buffer[BUFFER_SIZE]; |
| 25 | +static FILE *infile; |
| 26 | + |
| 27 | +int moveToNext() { |
| 28 | + fclose(infile); |
| 29 | + curSeek = 0ll; |
| 30 | + if (curIndex4Fnames + 1 >= fileAmount) { |
| 31 | + cout << "All files are read." << endl; |
| 32 | + return 0; |
| 33 | + } |
| 34 | + string fname = path + fnames[++curIndex4Fnames]; |
| 35 | + infile = fopen(fname.c_str(), "r"); |
| 36 | + if (!infile) { |
| 37 | + cerr << "Unable to open file: " << fname << endl; |
| 38 | + return -1; |
| 39 | + } |
| 40 | + return 1; |
| 41 | +} |
| 42 | + |
| 43 | +int readIOInit(int currank, int world_size, string filepath, string *filenames, int fileNum) { |
| 44 | + thisRank = currank; |
| 45 | + host_num = world_size; |
| 46 | + path = filepath; |
| 47 | + fnames = filenames; |
| 48 | + fileAmount = fileNum; |
| 49 | + curIndex4Fnames = -1; // 因为moveToNext需要直接增加 |
| 50 | + curSeek = 0ll; |
| 51 | + if (nullptr == filenames) { |
| 52 | + cerr << "No input read file." << endl; |
| 53 | + return -1; |
| 54 | + } |
| 55 | + while (moveToNext() == -1); |
| 56 | + if (curIndex4Fnames + 1 >= fileAmount) return -1; |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +int getNextRead(string *outread, size_t *readpos) { |
| 61 | + if (fileAmount < 1) return -1; |
| 62 | + // 先尝试读取,如果不能读取则转向下一个文件 |
| 63 | + while (fgets(buffer, BUFFER_SIZE, infile) == NULL) { |
| 64 | + int flag; |
| 65 | + while ((flag = moveToNext()) == -1) ; // 当读到的文件不能打开则马上打开下一个 |
| 66 | + if (flag == 0) return -1; // 全部读完则返回-1 |
| 67 | + } |
| 68 | + *readpos = curSeek; // 读取前的位置 |
| 69 | + fgetpos(infile, &curSeek); |
| 70 | + |
| 71 | + int len = strlen(buffer) - 1; |
| 72 | + *outread = string(buffer, len); |
| 73 | + |
| 74 | + return 1; |
| 75 | +} |
| 76 | + |
| 77 | +string getCurrentFilename() { |
| 78 | + return fnames[curIndex4Fnames]; |
| 79 | +} |
| 80 | + |
| 81 | +pthread_t requestWriteRead(const string &read) { |
| 82 | + pthread_t tid; |
| 83 | + if (pthread_create(&tid, NULL, writeReadRunner, (void *)&read) == -1) { |
| 84 | + cerr << "Error: Cannot create thread when requesting read writing." << endl; |
| 85 | + return 0; |
| 86 | + } |
| 87 | + pthread_detach(tid); |
| 88 | + return tid; |
| 89 | +} |
| 90 | + |
| 91 | +void *writeReadRunner(void *arg) { |
| 92 | + string read = *((string *) arg); |
| 93 | + if (createRead(read, nullptr) == 0) { |
| 94 | + cerr << "Error: Cannot create file for read \"" << read << "\"" << endl; |
| 95 | + return (void *) -1; |
| 96 | + } |
| 97 | + return (void *)0; |
| 98 | +} |
0 commit comments