00001 #include <sys/types.h>
00002 #include <sys/mman.h>
00003 #include <sys/stat.h>
00004 #include <fcntl.h>
00005 #include <math.h>
00006 #include <errno.h>
00007 #include <fstream>
00008 #include <Common/Exception.h>
00009 #include <Common/Util.h>
00010
00011 using namespace std;
00012 using namespace Util;
00013
00014
00015 int main(int argc, char *argv[]) {
00016 try {
00017 Exception::Assert( argc==3, "Usage: %s <matrix id> <dump file>\n", argv[0]);
00018 char *matrix_id = argv[1];
00019 char *out_file = argv[2];
00020
00021
00022 int mdesc = shm_open(matrix_id,O_RDONLY,0);
00023 Exception::Assert(mdesc>=0, "Error while opening shared memory: %s", strerror(errno));
00024
00025
00026 struct stat infos;
00027 int ret = fstat(mdesc,&infos);
00028 Exception::Assert(ret==0, "Error while retrieving size of shared memory: %s", strerror(errno));
00029
00030 int tot_space = infos.st_size;
00031 uint size = (uint)((-1. + sqrt(1.+8.*(double)(tot_space/sizeof(float))))/2.);
00032 cout << "Total allocated space: " << tot_space << endl;
00033 cout << "Matrix rows: " << size << endl;
00034
00035
00036 float* matrix = (float*)mmap(NULL,tot_space,PROT_READ,MAP_SHARED,mdesc,0);
00037 Exception::Assert( matrix!=(float*)-1, "Error while mapping shared memory: %s", strerror(errno));
00038
00039
00040 float* buffer = new float[size*size];
00041 cout << "Get matrix: " << flush;
00042 for(uint i=0; i<size; i++)
00043 for(uint l=0; l<=i; l++ )
00044 buffer[i*size+l] = buffer[l*size+i] = matrix[l+i*(i+1)/2];
00045
00046
00047 ofstream ofs(out_file); Exception::Assert(ofs, "Error while opening file %s", out_file);
00048
00049 for(uint i=0; i<size; i++) {
00050 outProgress(i,100);
00051 for(uint l=0; l<size; l++) {
00052 ofs << buffer[i*size+l];
00053 if( l<size-1 )
00054 ofs << " ";
00055 }
00056 ofs << endl;
00057 }
00058 endProgress(size);
00059
00060 ofs.close();
00061
00062
00063 ret = munmap(matrix,tot_space);
00064 Exception::Assert( ret==0, "Error while unmapping shared memory: %s", strerror(errno));
00065 }
00066 catch(Exception *e) {
00067 cerr << e->GetMessage() << endl;
00068 return 1;
00069 }
00070
00071 return 0;
00072 }