loadKernelMatrix.cpp

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/mman.h>
00003 #include <sys/stat.h>
00004 #include <limits.h>
00005 #include <stdlib.h>
00006 #include <fcntl.h>
00007 #include <errno.h>
00008 #include <fstream>
00009 #include "Exception.h"
00010 #include "Util.h"
00011 
00012 using namespace std;
00013 using namespace Util;
00014 
00015 void getSize(istream& is, uint& nrows, uint& ncols)
00016 {
00017   double dummy;
00018   ncols = 0;
00019   nrows = 0;
00020   string line;
00021 
00022   if(!getline(is,line))
00023     return;
00024   
00025   ++nrows;
00026   istringstream iss(line);
00027   while(iss >> dummy)
00028     ncols++;
00029    
00030   while( getline(is,line) ) {
00031     if( line == "")
00032       continue;
00033     ++nrows;
00034   }
00035 }
00036 
00037 
00038 int main(int argc, char *argv[]) {
00039   try {
00040     Exception::Assert( argc>=3, "Usage: %s <matrix file> <matrix id> [<symmetric=[0,1] (default=1)>]\n", argv[0]);
00041     char *in_file = argv[1];
00042     char *matrix_id = argv[2];
00043     bool symmetric = (argc>3) ? atoi(argv[3]) : true;
00044 
00045     // count lines
00046     ifstream ifs(in_file); Exception::Assert(ifs, "Error while opening file %s", in_file);
00047     
00048     cout << "Scan matrix: " << flush;
00049     uint nrows,ncols;
00050     getSize(ifs,nrows,ncols);
00051     cout << "found " << nrows << " rows and " << ncols << " cols" << endl;   
00052     ifs.close();
00053 
00054     // create shared memory
00055     int mdesc = shm_open(matrix_id,O_RDWR|O_CREAT|O_EXCL,0644);
00056     Exception::Assert(mdesc>=0, "Error while creating shared memory: %s", strerror(errno));
00057 
00058     // make space
00059     int tot_space = (symmetric) ? sizeof(float)*nrows*(nrows+1)/2 : sizeof(float)*nrows*ncols;
00060     int ret = ftruncate(mdesc,tot_space);
00061     Exception::Assert(ret==0, "Error while allocating shared memory: %s", strerror(errno));
00062 
00063     // map to memory
00064     float* matrix = (float*)mmap(NULL,tot_space,PROT_WRITE,MAP_SHARED,mdesc,0);
00065     Exception::Assert( matrix!=(float*)-1, "Error while mapping shared memory: %s", strerror(errno));
00066     
00067     // fill matrix
00068 
00069     ifstream ims(in_file); Exception::Assert(ims, "Error while opening file %s", in_file);
00070     string line;
00071     cout << "Read matrix: " << flush;
00072     if(symmetric){
00073       for(uint i=0; i<nrows; i++) {
00074         outProgress(i,100);
00075 
00076         getline(ims,line);
00077         istringstream ss(line);
00078         for(uint l=0; l<=i; l++ ) {
00079           matrix[l+i*(i+1)/2] = get<float>(ss);      
00080         }
00081       }
00082     }
00083     else{
00084       for(uint i=0; i<nrows; i++) {
00085         outProgress(i,100);
00086 
00087         getline(ims,line);
00088         istringstream ss(line);
00089         for(uint l=0; l< ncols; l++ ) {
00090           matrix[i*ncols+l] = get<float>(ss);      
00091         }
00092       }      
00093     }
00094     endProgress(nrows);
00095 
00096     ims.close();
00097 
00098     // unmap memory
00099     ret = munmap(matrix,tot_space);
00100     Exception::Assert( ret==0, "Error while unmapping shared memory: %s", strerror(errno));
00101   }
00102   catch(Exception *e) {
00103     cerr << e->GetMessage() << endl;
00104     return 1;
00105   }
00106 
00107   return 0;
00108 }

Generated on Fri Sep 7 16:29:17 2007 for SVM-Dlight by  doxygen 1.5.1