首页 / C++ / C++ 对TXT 的串并行读写
C++ 对TXT 的串并行读写
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 对TXT 的串并行读写,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2525字,纯文字阅读大概需要4分钟。
内容图文
任务说明:有36篇文档,现在要读入,并统计词频,字典长度25,希望能够比较串并行读写操作的时间差距。
-
串行读入并统计词频
// LoadDocsInUbuntu.cpp // #include <iostream> #include <stdio.h> #include <vector> using namespace std; int main() { char filename[100]; size_t d; FILE *fileptr; int word; vector< vector<int> > corpus; printf("load data ...\n"); for (d = 1; d < 37; d++){ sprintf(filename, "..//data/doc_%d.txt", d); fileptr = fopen(filename, "r"); vector<int> doc; int ff[25] = { 0 }; while (fscanf(fileptr, "%d", &word) != EOF) { ff[word - 1] = ff[word - 1] + 1; doc.push_back(word); } corpus.push_back(doc); fclose(fileptr); sprintf(filename, "..//result/freqUbuntuSerial_%d.txt", d); fileptr = fopen(filename, "w"); for (int f = 0; f < 25; f++) { fprintf(fileptr, "%d ", ff[f]); } fclose(fileptr); } cout <<"corpus.size()="<< corpus.size() << endl; return 0; }
-
这里讨论并行有三种思路:一,按照文档序号进行分组读入统计等操作;二,在文档内按单词数目分组进行统计;三,将统计与读写操作并行处理。
针对第一种思路,使用openmp做多线程处理:
// LoadDocsByOpenMP.cpp // #include <omp.h> #include <iostream> #include <stdio.h> #include <vector> #include <stdlib.h> #include <time.h> #include <string> using namespace std; int main() { char filename[100],resultname[100]; int d; FILE *fileptr[360]; int word; int ff[360][25] = { 0 }; //vector< vector<int> > corpus; clock_t start,finish; int f[360]={0}; start=clock(); printf("load data ...\n"); #pragma omp parallel for num_threads(4) for (d = 1; d < 361; d++){ printf("Hello world, I am %d, docs index %d.\n",omp_get_thread_num(),d); sprintf(filename, "..//data/doc_%d.txt", d); fileptr[d-1] = fopen(filename, "r"); //int ff[25]={0}; ////vector<int> doc; while (fscanf(fileptr[d-1], "%d", &word) != EOF) { ff[d-1][word - 1] = ff[d-1][word - 1] + 1; //ff[word-1]=ff[word-1]+1; // //doc.push_back(word); } ////corpus.push_back(doc); fclose(fileptr[d-1]); sprintf(resultname, "..//result/freqByOpenMP_%d.txt", d);//Be CAREFUL!For the name "filename" has been used before, we must name the string differently here. fileptr[d-1] = fopen(resultname, "w"); for (f[d-1] = 0; f[d-1] < 25; f[d-1]++) { fprintf(fileptr[d-1], "%d ", ff[f[d-1]]); } fclose(fileptr[d-1]); } //cout <<"corpus.size()="<< corpus.size() << endl; finish=clock(); cout<<"time cost : "<< (double)(finish-start)/ CLOCKS_PER_SEC<<endl; return 0; }
但初步比较openmp对串行读取的速度并没有太多提升,反而是当进程数多于系统物理核数的时候,程序时间会加长。
另外两种实现思路在后续学习中继续实现。
原文:http://www.cnblogs.com/simayuhe/p/7208393.html
内容总结
以上是互联网集市为您收集整理的C++ 对TXT 的串并行读写全部内容,希望文章能够帮你解决C++ 对TXT 的串并行读写所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。