svmDlight_demon.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 <fcntl.h>
00005 #include <errno.h>
00006 #include <math.h>
00007 #include <fstream>
00008 #include <assert.h>
00009 #include <sstream>
00010 #include "Util.h"
00011 #include "Exception.h"
00012 #include "svector.h"
00013 
00024 using namespace std;
00025 using namespace Util;
00026 
00027 static void *K;
00028 static uint  size;
00029 static bool fixed_point;
00030 
00031 extern "C" double sprod_data(void *da, void *db) { 
00032   try {
00033     uint &i = *(uint*)da;
00034     uint &l = *(uint*)db;    
00035     Exception::Assert(i<size, "Invalid row index %d", i);
00036     Exception::Assert(l<size, "Invalid col index %d", l);
00037 
00038     if( fixed_point ) {
00039       return tofloat((i>=l) ?((short*)K)[l+i*(i+1)/2] : ((short*)K)[i+l*(l+1)/2]);
00040     }
00041     else
00042       return (i>=l) ?((float*)K)[l+i*(i+1)/2] : ((float*)K)[i+l*(l+1)/2];
00043   }
00044   catch(Exception *e) {
00045     cerr << e->GetMessage() << endl;
00046     assert(0);
00047   }
00048 }
00049 
00050 extern "C" double plugin_kernel(void *da, void *db) {
00051   try {
00052     return sprod_data(((SVECTOR*)da)->words,((SVECTOR*)db)->words);
00053   }
00054   catch(Exception *e) {
00055     cerr << e->GetMessage() << endl;
00056     assert(0);
00057   }
00058 }
00059 
00060 extern "C" void* 
00061 plugin_parse_document(char *line, double *label,
00062                       long *queryid, long *slackid, double *costfactor,
00063                       long int *highest_feature, 
00064                       long int max_words_doc,
00065                       char **comment)
00066 {
00067 
00068   register long pos;
00069   char buffer[1024];
00070 
00071   (*queryid)=0;
00072   (*slackid)=0;
00073   (*costfactor)=1;
00074   (*highest_feature) = 1;
00075 
00076   pos=0;
00077   (*comment)=NULL;
00078   while(line[pos] ) {      /* cut off comments */
00079     if((line[pos] == '#') && (!(*comment))) {
00080       line[pos]=0;
00081       (*comment)=&(line[pos+1]);
00082     }
00083     if(line[pos] == '\n') { /* strip the CR */
00084       line[pos]=0;
00085     }
00086     pos++;
00087   }
00088   if(!(*comment)) (*comment)=&(line[pos]);
00089   /* printf("Comment: '%s'\n",(*comment)); */
00090 
00091 
00092   if(sscanf(line,"%s",buffer) == EOF) return(NULL);
00093   pos=0;
00094   /* read the target value */
00095   if(sscanf(line,"%lf",label) == EOF) return(NULL);
00096   /* printf("label: '%f'\n",(float)(*label)); */
00097   pos=0;
00098   //  while(space_or_null((int)line[pos])) pos++;
00099   //  while((!space_or_null((int)line[pos])) && line[pos]) pos++;
00100   while(line[pos] && isspace((int)line[pos])) pos++;
00101   while(line[pos] && !isspace((int)line[pos])) pos++; // skip label
00102   while(line[pos] && isspace((int)line[pos])) pos++;
00103   /* read the qid if present (used for ranking) */ 
00104   if(sscanf(line+pos,"qid:%ld",queryid)==1){
00105     while(line[pos] && !isspace((int)line[pos])) pos++; // skip qid
00106     while(line[pos] && isspace((int)line[pos])) pos++;
00107   }
00108   /* read the id value */
00109   uint *d = new uint;
00110   if(sscanf(line+pos,"%u",d) == EOF) return(NULL);
00111 
00112   //printf("label:%lf qid:%ld id:%ld\n", *label,*queryid,*d);
00113 
00114   return d;
00115 }
00116 
00117 
00118 extern "C" void plugin_write(FILE* os, void* d) 
00119 {
00120   fprintf(os, "%u", *(uint*)d);
00121 }
00122 
00123 extern "C"  void* plugin_copy_constructor(void* _d)
00124 {
00125   uint *d = new uint;
00126   *d = *(uint*)_d;
00127   return d;  
00128 }
00129 
00130 extern "C" void plugin_destructor(void* d) {
00131   delete (uint*)d;
00132 }
00133 
00134 extern "C" void* plugin_null(void) 
00135 {
00136   Exception::Assert(false, "A null id does not exist!");
00137   return NULL;
00138 }
00139 
00140 extern "C" void plugin_kernel_setparm(char *kernel_parm) {
00141   try {
00142     if( strcmp(kernel_parm,"empty") ) {
00143       vector<string> tokens = tokenize(kernel_parm,":");            
00144       string matrix_id = trim(tokens[0]);
00145       fixed_point = (tokens.size()>1) ?cast<bool>(tokens[1]) :false;      
00146       
00147       cout << "Using shared matrix: " << matrix_id << " " << fixed_point << endl;
00148 
00149       // open shared memory
00150       int mdesc = shm_open(matrix_id.c_str(),O_RDONLY,0);
00151       Exception::Assert(mdesc>=0, "Error while opening shared memory: %s", strerror(errno));
00152 
00153       // get size
00154       struct stat infos;
00155       int ret = fstat(mdesc,&infos);
00156       Exception::Assert(ret==0, "Error while retrieving size of shared memory: %s", strerror(errno));
00157 
00158       size_t tot_space = infos.st_size;
00159       if( fixed_point )
00160         size = (uint)((-1. + sqrt(1.+8.*(double)(tot_space/sizeof(short))))/2.);
00161       else
00162         size = (uint)((-1. + sqrt(1.+8.*(double)(tot_space/sizeof(float))))/2.);
00163       cout << "Total allocated space: " << tot_space << endl;
00164       cout << "Matrix rows: " << size << endl;
00165 
00166       // map to memory
00167       K = mmap(NULL,tot_space,PROT_READ,MAP_SHARED,mdesc,0);
00168       Exception::Assert( K!=(void*)-1, "Error while mapping shared memory: %s", strerror(errno));    
00169     }
00170     else {
00171       K = NULL;
00172       size = 0;
00173     }
00174   }
00175   catch(Exception *e) {
00176     cerr << e->GetMessage() << endl;
00177     assert(0);
00178   }
00179 }
00180 
00181 extern "C" void
00182 plugin_clip(void* _words, int totwords)
00183 {
00184 
00185 }
00186 
00187 int main(int argc, char *argv[]) {
00188 }

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