C++ std::使用2个mmaped数组合并?
我正在对两个文本文件进行mmaping,每行写一个整数。 我从驱动器中读取它们,我想对它们进行排序合并。 两个输入文件“1piece0”和“1piece1”有一个排序整数列表。 输出文件的大小与两个文件的大小相同,但不是那么多整数。 问题:两个输入文件有2543000行,而输出文件应该有50860000行,但只有17259463行。 这是我当前的代码C++ std::使用2个mmaped数组合并?,c++,c,unix,stl,mmap,C++,C,Unix,Stl,Mmap,我正在对两个文本文件进行mmaping,每行写一个整数。 我从驱动器中读取它们,我想对它们进行排序合并。 两个输入文件“1piece0”和“1piece1”有一个排序整数列表。 输出文件的大小与两个文件的大小相同,但不是那么多整数。 问题:两个输入文件有2543000行,而输出文件应该有50860000行,但只有17259463行。 这是我当前的代码 #include <stdio.h> #include <stdlib.h> #include <sys/types
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <algorithm>
#define FILESIZE 25430000 * sizeof(int)
#define FILE0 279288034
#define FILE1 279287226
int main()
{
int i;
int fd;
int fd2;
int fd3;
int result;
int *map;
int *map2;
int *map3;
fd3 = open( "file.out", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0755);
if ( fd3 == -1 ) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
result = lseek(fd3, FILE0 + FILE1 - 1, SEEK_SET );
if(result == -1) {
close(fd);
perror("Error calling lseek\n");
exit(EXIT_FAILURE);
}
result = write(fd3,"",1);
if( result != 1 ) {
close(fd3);
perror("error writing last byte");
exit(EXIT_FAILURE);
}
map3 =(int *) mmap(0, FILE0 + FILE1, PROT_READ | PROT_WRITE, MAP_SHARED, fd3, 0);
if( map == MAP_FAILED ) {
close(fd);
perror("Error mmapinG fd3");
exit(EXIT_FAILURE);
}
fd = open( "1piece0", O_RDONLY );
if( fd == -1 ) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
map = (int *)mmap(0, FILE0, PROT_READ, MAP_SHARED, fd, 0 );
if( map == MAP_FAILED ) {
close(fd);
perror("error mapping file");
exit(EXIT_FAILURE);
}
fd2 = open( "1piece1", O_RDONLY );
if( fd2 == -1 ) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
map2 = (int *)mmap(0, FILE1, PROT_READ, MAP_SHARED, fd2, 0 );
if( map == MAP_FAILED ) {
close(fd2);
perror("error mapping file");
exit(EXIT_FAILURE);
}
// while(1);
std::merge( map, map + 25430000, map2, map2 + 25430000, map3 );
if(munmap(map, FILE0 ) == -1 ) {
perror("error unmapping map");
}
close(fd);
if(munmap(map3, FILE0 + FILE1 ) == -1 ) {
perror("error unmapping map3");
}
close(fd3);
if(munmap(map2, FILE1 ) == -1 ) {
perror("error unmapping map2");
}
close(fd2);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义文件大小2543000*sizeof(int)
#定义文件0 27928034
#定义文件1 279287226
int main()
{
int i;
int-fd;
int-fd2;
int-fd3;
int结果;
int*地图;
int*map2;
int*map3;
fd3=打开(“file.out”,O|RDWR | O|CREAT | O|TRUNC,(模式)0755);
如果(fd3==-1){
perror(“打开文件进行写入时出错”);
退出(退出失败);
}
结果=lseek(fd3,文件0+文件1-1,搜索集);
如果(结果==-1){
关闭(fd);
perror(“调用lseek时出错\n”);
退出(退出失败);
}
结果=写入(fd3,“,1);
如果(结果!=1){
关闭(fd3);
perror(“写入最后一个字节时出错”);
退出(退出失败);
}
map3=(int*)mmap(0,FILE0+FILE1,PROT_READ | PROT_WRITE,MAP_SHARED,fd3,0);
if(map==map\u失败){
关闭(fd);
perror(“错误mmapinG fd3”);
退出(退出失败);
}
fd=打开(“1件0”,仅限Ordu);
如果(fd==-1){
perror(“打开文件进行写入时出错”);
退出(退出失败);
}
map=(int*)mmap(0,FILE0,PROT_READ,map_SHARED,fd,0);
if(map==map\u失败){
关闭(fd);
perror(“错误映射文件”);
退出(退出失败);
}
fd2=打开(“1件1”,仅限O_rdu);
如果(fd2==-1){
perror(“打开文件进行写入时出错”);
退出(退出失败);
}
map2=(int*)mmap(0,FILE1,PROT_READ,MAP_SHARED,fd2,0);
if(map==map\u失败){
关闭(fd2);
perror(“错误映射文件”);
退出(退出失败);
}
//而(1),;
合并(地图,地图+2543000,地图2,地图2+2543000,地图3);
if(munmap(map,FILE0)=-1){
perror(“错误取消映射映射”);
}
关闭(fd);
if(munmap(map3,FILE0+FILE1)=-1){
perror(“错误取消映射map3”);
}
关闭(fd3);
if(munmap(map2,FILE1)=-1){
perror(“错误取消映射map2”);
}
关闭(fd2);
返回0;
}
你能告诉我我做错了什么吗
更新:行是指整数,然后是换行符。你说的“行”是什么意思
当您使用内存映射时,它将数据视为内存,而在这里,您将读取它,就像读取整数数组一样。因此,输入必须为本机二进制格式(即,字节以相同的方式、相同的大小和相同的结尾存储),2543000是从每个集合中读取的INT数
这就是你输入的存储方式吗
这里有很多“神奇数字”。您不能将文本行视为二进制BLOB,并将其作为int指针进行操作 您可以将文本文件视为以下文本:
void merge\u ints(std::istream&a\u in,std::istream&b\u in,std::ostream&out){
INTA,b;
std::istream*剩余=0;
如果(!(a_in>>a)){
剩余=&b_英寸;
}
否则如果(!(b_in>>b)){
out告诉我们出了什么问题。@Jens-文件的格式是整数,然后是换行符。所以我想对它们进行排序合并。我想我无法对它们进行mmap。确切的问题是输出文件中似乎没有足够的行。map2和map3旁边的if条件不应该检查map2和map吗3,而不是map?它是一个文本文件。我应该将其转换为二进制文件吗?我该怎么做?如果你想这样做,你需要这样做,但你可以使用流来读取文本。最简单的方法就是在打开的文件句柄(ifstream)上使用istream_迭代器作为您的输入迭代器类型。我不想使用流的主要原因是它们太慢。我希望程序尽可能快。我只有100mb的主内存,所以我想mmap它们,但我想我必须编写代码,将它们存储在内存块中并执行合并。C文件I/O更快,不是吗?我不应该吗你为什么不测试一下呢?使用scanf系列也可以。主要的一点是如果你有文本数据,你就不能把它当作非文本(“二进制”)数据。它速度更快,但没有我希望的那么快。谢谢你的澄清:)。
void merge_ints(std::istream &a_in, std::istream &b_in, std::ostream &out) {
int a, b;
std::istream *remaining = 0;
if (!(a_in >> a)) {
remaining = &b_in;
}
else if (!(b_in >> b)) {
out << a << '\n';
remaining = &a_in;
}
else while (a_in && b_in) {
if (a < b) {
out << a << '\n';
if (!(a_in >> a)) {
out << b << '\n';
remaining = &b_in;
}
}
else {
out << b << '\n';
if (!(b_in >> b)) {
out << a << '\n';
remaining = &a_in;
}
}
}
for (int x; *remaining >> x;) {
out << x << '\n';
}
}
void merge_ints(std::istream &a, std::istream &b, std::ostream &out) {
typedef std::istream_iterator<int> In;
std::merge(In(a), In(), In(b), In(), std::ostream_iterator<int>(out, "\n"));
}
int main() {
stringstream a ("1\n3\n5\n"), b ("2\n4\n6\n7\n"), out;
merge_ints(a, b, out);
cout << out.str();
}