SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Kernel.cpp
Go to the documentation of this file.
1 /*
2  * EXCEPT FOR THE KERNEL CACHING FUNCTIONS WHICH ARE (W) THORSTEN JOACHIMS
3  * COPYRIGHT (C) 1999 UNIVERSITAET DORTMUND - ALL RIGHTS RESERVED
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Written (W) 1999-2009 Soeren Sonnenburg
11  * Written (W) 1999-2008 Gunnar Raetsch
12  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
13  */
14 
15 #include <shogun/lib/config.h>
16 #include <shogun/lib/common.h>
17 #include <shogun/io/SGIO.h>
18 #include <shogun/io/File.h>
19 #include <shogun/lib/Time.h>
20 #include <shogun/lib/Signal.h>
21 
22 #include <shogun/base/Parallel.h>
23 
24 #include <shogun/kernel/Kernel.h>
27 #include <shogun/base/Parameter.h>
28 
30 
31 #include <string.h>
32 #include <unistd.h>
34 
35 #ifdef HAVE_PTHREAD
36 #include <pthread.h>
37 #endif
38 
39 using namespace shogun;
40 
42 {
43  init();
45 }
46 
47 CKernel::CKernel(int32_t size) : CSGObject()
48 {
49  init();
50 
51  if (size<10)
52  size=10;
53 
54  cache_size=size;
56 }
57 
58 
59 CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, int32_t size) : CSGObject()
60 {
61  init();
62 
63  if (size<10)
64  size=10;
65 
66  cache_size=size;
67 
69  init(p_lhs, p_rhs);
71 }
72 
74 {
75  if (get_is_initialized())
76  SG_ERROR("Kernel still initialized on destruction.\n")
77 
80 
81  SG_INFO("Kernel deleted (%p).\n", this)
82 }
83 
84 #ifdef USE_SVMLIGHT
85 void CKernel::resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack)
86 {
87  if (size<10)
88  size=10;
89 
91  cache_size=size;
92 
93  if (has_features() && get_num_vec_lhs())
94  kernel_cache_init(cache_size, regression_hack);
95 }
96 #endif //USE_SVMLIGHT
97 
98 bool CKernel::init(CFeatures* l, CFeatures* r)
99 {
100  /* make sure that features are not deleted if same ones are used */
101  SG_REF(l);
102  SG_REF(r);
103 
104  //make sure features were indeed supplied
105  REQUIRE(l, "CKernel::init(%p, %p): Left hand side features required!\n", l, r)
106  REQUIRE(r, "CKernel::init(%p, %p): Right hand side features required!\n", l, r)
107 
108  //make sure features are compatible
111 
112  //remove references to previous features
114 
115  //increase reference counts
116  SG_REF(l);
117  if (l==r)
118  lhs_equals_rhs=true;
119  else // l!=r
120  SG_REF(r);
121 
122  lhs=l;
123  rhs=r;
124 
127 
130 
131  /* unref "safety" refs from beginning */
132  SG_UNREF(r);
133  SG_UNREF(l);
134 
135  SG_DEBUG("leaving CKernel::init(%p, %p)\n", l, r)
136  return true;
137 }
138 
140 {
141  SG_REF(n);
142  if (lhs && rhs)
143  n->init(this);
144 
146  normalizer=n;
147 
148  return (normalizer!=NULL);
149 }
150 
152 {
154  return normalizer;
155 }
156 
158 {
159  return normalizer->init(this);
160 }
161 
163 {
165 }
166 
167 #ifdef USE_SVMLIGHT
168 /****************************** Cache handling *******************************/
169 
170 void CKernel::kernel_cache_init(int32_t buffsize, bool regression_hack)
171 {
172  int32_t totdoc=get_num_vec_lhs();
173  if (totdoc<=0)
174  {
175  SG_ERROR("kernel has zero rows: num_lhs=%d num_rhs=%d\n",
177  }
178  uint64_t buffer_size=0;
179  int32_t i;
180 
181  //in regression the additional constraints are made by doubling the training data
182  if (regression_hack)
183  totdoc*=2;
184 
185  buffer_size=((uint64_t) buffsize)*1024*1024/sizeof(KERNELCACHE_ELEM);
186  if (buffer_size>((uint64_t) totdoc)*totdoc)
187  buffer_size=((uint64_t) totdoc)*totdoc;
188 
189  SG_INFO("using a kernel cache of size %lld MB (%lld bytes) for %s Kernel\n", buffer_size*sizeof(KERNELCACHE_ELEM)/1024/1024, buffer_size*sizeof(KERNELCACHE_ELEM), get_name())
190 
191  //make sure it fits in the *signed* KERNELCACHE_IDX type
192  ASSERT(buffer_size < (((uint64_t) 1) << (sizeof(KERNELCACHE_IDX)*8-1)))
193 
194  kernel_cache.index = SG_MALLOC(int32_t, totdoc);
195  kernel_cache.occu = SG_MALLOC(int32_t, totdoc);
196  kernel_cache.lru = SG_MALLOC(int32_t, totdoc);
197  kernel_cache.invindex = SG_MALLOC(int32_t, totdoc);
198  kernel_cache.active2totdoc = SG_MALLOC(int32_t, totdoc);
199  kernel_cache.totdoc2active = SG_MALLOC(int32_t, totdoc);
200  kernel_cache.buffer = SG_MALLOC(KERNELCACHE_ELEM, buffer_size);
201  kernel_cache.buffsize=buffer_size;
202  kernel_cache.max_elems=(int32_t) (kernel_cache.buffsize/totdoc);
203 
204  if(kernel_cache.max_elems>totdoc) {
205  kernel_cache.max_elems=totdoc;
206  }
207 
208  kernel_cache.elems=0; // initialize cache
209  for(i=0;i<totdoc;i++) {
210  kernel_cache.index[i]=-1;
211  kernel_cache.lru[i]=0;
212  }
213  for(i=0;i<totdoc;i++) {
214  kernel_cache.occu[i]=0;
215  kernel_cache.invindex[i]=-1;
216  }
217 
218  kernel_cache.activenum=totdoc;;
219  for(i=0;i<totdoc;i++) {
220  kernel_cache.active2totdoc[i]=i;
221  kernel_cache.totdoc2active[i]=i;
222  }
223 
224  kernel_cache.time=0;
225 }
226 
228  int32_t docnum, int32_t *active2dnum, float64_t *buffer, bool full_line)
229 {
230  int32_t i,j;
231  KERNELCACHE_IDX start;
232 
233  int32_t num_vectors = get_num_vec_lhs();
234  if (docnum>=num_vectors)
235  docnum=2*num_vectors-1-docnum;
236 
237  /* is cached? */
238  if(kernel_cache.index[docnum] != -1)
239  {
240  kernel_cache.lru[kernel_cache.index[docnum]]=kernel_cache.time; /* lru */
241  start=((KERNELCACHE_IDX) kernel_cache.activenum)*kernel_cache.index[docnum];
242 
243  if (full_line)
244  {
245  for(j=0;j<get_num_vec_lhs();j++)
246  {
247  if(kernel_cache.totdoc2active[j] >= 0)
248  buffer[j]=kernel_cache.buffer[start+kernel_cache.totdoc2active[j]];
249  else
250  buffer[j]=(float64_t) kernel(docnum, j);
251  }
252  }
253  else
254  {
255  for(i=0;(j=active2dnum[i])>=0;i++)
256  {
257  if(kernel_cache.totdoc2active[j] >= 0)
258  buffer[j]=kernel_cache.buffer[start+kernel_cache.totdoc2active[j]];
259  else
260  {
261  int32_t k=j;
262  if (k>=num_vectors)
263  k=2*num_vectors-1-k;
264  buffer[j]=(float64_t) kernel(docnum, k);
265  }
266  }
267  }
268  }
269  else
270  {
271  if (full_line)
272  {
273  for(j=0;j<get_num_vec_lhs();j++)
274  buffer[j]=(KERNELCACHE_ELEM) kernel(docnum, j);
275  }
276  else
277  {
278  for(i=0;(j=active2dnum[i])>=0;i++)
279  {
280  int32_t k=j;
281  if (k>=num_vectors)
282  k=2*num_vectors-1-k;
283  buffer[j]=(KERNELCACHE_ELEM) kernel(docnum, k);
284  }
285  }
286  }
287 }
288 
289 
290 // Fills cache for the row m
292 {
293  register int32_t j,k,l;
294  register KERNELCACHE_ELEM *cache;
295 
296  int32_t num_vectors = get_num_vec_lhs();
297 
298  if (m>=num_vectors)
299  m=2*num_vectors-1-m;
300 
301  if(!kernel_cache_check(m)) // not cached yet
302  {
303  cache = kernel_cache_clean_and_malloc(m);
304  if(cache) {
305  l=kernel_cache.totdoc2active[m];
306 
307  for(j=0;j<kernel_cache.activenum;j++) // fill cache
308  {
309  k=kernel_cache.active2totdoc[j];
310 
311  if((kernel_cache.index[k] != -1) && (l != -1) && (k != m)) {
312  cache[j]=kernel_cache.buffer[((KERNELCACHE_IDX) kernel_cache.activenum)
313  *kernel_cache.index[k]+l];
314  }
315  else
316  {
317  if (k>=num_vectors)
318  k=2*num_vectors-1-k;
319 
320  cache[j]=kernel(m, k);
321  }
322  }
323  }
324  else
325  perror("Error: Kernel cache full! => increase cache size");
326  }
327 }
328 
329 
330 void* CKernel::cache_multiple_kernel_row_helper(void* p)
331 {
332  int32_t j,k,l;
333  S_KTHREAD_PARAM* params = (S_KTHREAD_PARAM*) p;
334 
335  for (int32_t i=params->start; i<params->end; i++)
336  {
337  KERNELCACHE_ELEM* cache=params->cache[i];
338  int32_t m = params->uncached_rows[i];
339  l=params->kernel_cache->totdoc2active[m];
340 
341  for(j=0;j<params->kernel_cache->activenum;j++) // fill cache
342  {
343  k=params->kernel_cache->active2totdoc[j];
344 
345  if((params->kernel_cache->index[k] != -1) && (l != -1) && (!params->needs_computation[k])) {
346  cache[j]=params->kernel_cache->buffer[((KERNELCACHE_IDX) params->kernel_cache->activenum)
347  *params->kernel_cache->index[k]+l];
348  }
349  else
350  {
351  if (k>=params->num_vectors)
352  k=2*params->num_vectors-1-k;
353 
354  cache[j]=params->kernel->kernel(m, k);
355  }
356  }
357 
358  //now line m is cached
359  params->needs_computation[m]=0;
360  }
361  return NULL;
362 }
363 
364 // Fills cache for the rows in key
365 void CKernel::cache_multiple_kernel_rows(int32_t* rows, int32_t num_rows)
366 {
367 #ifdef HAVE_PTHREAD
368  int32_t nthreads=parallel->get_num_threads();
369 
370  if (nthreads<2)
371  {
372 #endif
373  for(int32_t i=0;i<num_rows;i++)
374  cache_kernel_row(rows[i]);
375 #ifdef HAVE_PTHREAD
376  }
377  else
378  {
379  // fill up kernel cache
380  int32_t* uncached_rows = SG_MALLOC(int32_t, num_rows);
381  KERNELCACHE_ELEM** cache = SG_MALLOC(KERNELCACHE_ELEM*, num_rows);
382  pthread_t* threads = SG_MALLOC(pthread_t, nthreads-1);
383  S_KTHREAD_PARAM* params = SG_MALLOC(S_KTHREAD_PARAM, nthreads-1);
384  int32_t num_threads=nthreads-1;
385  int32_t num_vec=get_num_vec_lhs();
386  ASSERT(num_vec>0)
387  uint8_t* needs_computation=SG_CALLOC(uint8_t, num_vec);
388 
389  int32_t step=0;
390  int32_t num=0;
391  int32_t end=0;
392 
393  // allocate cachelines if necessary
394  for (int32_t i=0; i<num_rows; i++)
395  {
396  int32_t idx=rows[i];
397  if (idx>=num_vec)
398  idx=2*num_vec-1-idx;
399 
400  if (kernel_cache_check(idx))
401  continue;
402 
403  needs_computation[idx]=1;
404  uncached_rows[num]=idx;
405  cache[num]= kernel_cache_clean_and_malloc(idx);
406 
407  if (!cache[num])
408  SG_ERROR("Kernel cache full! => increase cache size\n")
409 
410  num++;
411  }
412 
413  if (num>0)
414  {
415  step= num/nthreads;
416 
417  if (step<1)
418  {
419  num_threads=num-1;
420  step=1;
421  }
422 
423  for (int32_t t=0; t<num_threads; t++)
424  {
425  params[t].kernel = this;
426  params[t].kernel_cache = &kernel_cache;
427  params[t].cache = cache;
428  params[t].uncached_rows = uncached_rows;
429  params[t].needs_computation = needs_computation;
430  params[t].num_uncached = num;
431  params[t].start = t*step;
432  params[t].end = (t+1)*step;
433  params[t].num_vectors = get_num_vec_lhs();
434  end=params[t].end;
435 
436  int code=pthread_create(&threads[t], NULL,
437  CKernel::cache_multiple_kernel_row_helper, (void*)&params[t]);
438 
439  if (code != 0)
440  {
441  SG_WARNING("Thread creation failed (thread %d of %d) "
442  "with error:'%s'\n",t, num_threads, strerror(code));
443  num_threads=t;
444  end=t*step;
445  break;
446  }
447  }
448  }
449  else
450  num_threads=-1;
451 
452 
453  S_KTHREAD_PARAM last_param;
454  last_param.kernel = this;
455  last_param.kernel_cache = &kernel_cache;
456  last_param.cache = cache;
457  last_param.uncached_rows = uncached_rows;
458  last_param.needs_computation = needs_computation;
459  last_param.start = end;
460  last_param.num_uncached = num;
461  last_param.end = num;
462  last_param.num_vectors = get_num_vec_lhs();
463 
464  cache_multiple_kernel_row_helper(&last_param);
465 
466 
467  for (int32_t t=0; t<num_threads; t++)
468  {
469  if (pthread_join(threads[t], NULL) != 0)
470  SG_WARNING("pthread_join of thread %d/%d failed\n", t, num_threads)
471  }
472 
473  SG_FREE(needs_computation);
474  SG_FREE(params);
475  SG_FREE(threads);
476  SG_FREE(cache);
477  SG_FREE(uncached_rows);
478  }
479 #endif
480 }
481 
482 // remove numshrink columns in the cache
483 // which correspond to examples marked
485  int32_t totdoc, int32_t numshrink, int32_t *after)
486 {
487  ASSERT(totdoc > 0);
488  register int32_t i,j,jj,scount; // 0 in after.
489  KERNELCACHE_IDX from=0,to=0;
490  int32_t *keep;
491 
492  keep=SG_MALLOC(int32_t, totdoc);
493  for(j=0;j<totdoc;j++) {
494  keep[j]=1;
495  }
496  scount=0;
497  for(jj=0;(jj<kernel_cache.activenum) && (scount<numshrink);jj++) {
498  j=kernel_cache.active2totdoc[jj];
499  if(!after[j]) {
500  scount++;
501  keep[j]=0;
502  }
503  }
504 
505  for(i=0;i<kernel_cache.max_elems;i++) {
506  for(jj=0;jj<kernel_cache.activenum;jj++) {
507  j=kernel_cache.active2totdoc[jj];
508  if(!keep[j]) {
509  from++;
510  }
511  else {
512  kernel_cache.buffer[to]=kernel_cache.buffer[from];
513  to++;
514  from++;
515  }
516  }
517  }
518 
519  kernel_cache.activenum=0;
520  for(j=0;j<totdoc;j++) {
521  if((keep[j]) && (kernel_cache.totdoc2active[j] != -1)) {
522  kernel_cache.active2totdoc[kernel_cache.activenum]=j;
523  kernel_cache.totdoc2active[j]=kernel_cache.activenum;
524  kernel_cache.activenum++;
525  }
526  else {
527  kernel_cache.totdoc2active[j]=-1;
528  }
529  }
530 
531  kernel_cache.max_elems= (int32_t) kernel_cache.buffsize;
532 
533  if (kernel_cache.activenum>0)
534  kernel_cache.buffsize/=kernel_cache.activenum;
535 
536  if(kernel_cache.max_elems>totdoc)
537  kernel_cache.max_elems=totdoc;
538 
539  SG_FREE(keep);
540 
541 }
542 
544 {
545  int32_t maxlru=0,k;
546 
547  for(k=0;k<kernel_cache.max_elems;k++) {
548  if(maxlru < kernel_cache.lru[k])
549  maxlru=kernel_cache.lru[k];
550  }
551  for(k=0;k<kernel_cache.max_elems;k++) {
552  kernel_cache.lru[k]-=maxlru;
553  }
554 }
555 
557 {
558  SG_FREE(kernel_cache.index);
559  SG_FREE(kernel_cache.occu);
560  SG_FREE(kernel_cache.lru);
561  SG_FREE(kernel_cache.invindex);
562  SG_FREE(kernel_cache.active2totdoc);
563  SG_FREE(kernel_cache.totdoc2active);
564  SG_FREE(kernel_cache.buffer);
565  memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
566 }
567 
568 int32_t CKernel::kernel_cache_malloc()
569 {
570  int32_t i;
571 
573  for(i=0;i<kernel_cache.max_elems;i++) {
574  if(!kernel_cache.occu[i]) {
575  kernel_cache.occu[i]=1;
576  kernel_cache.elems++;
577  return(i);
578  }
579  }
580  }
581  return(-1);
582 }
583 
584 void CKernel::kernel_cache_free(int32_t cacheidx)
585 {
586  kernel_cache.occu[cacheidx]=0;
587  kernel_cache.elems--;
588 }
589 
590 // remove least recently used cache
591 // element
592 int32_t CKernel::kernel_cache_free_lru()
593 {
594  register int32_t k,least_elem=-1,least_time;
595 
596  least_time=kernel_cache.time+1;
597  for(k=0;k<kernel_cache.max_elems;k++) {
598  if(kernel_cache.invindex[k] != -1) {
599  if(kernel_cache.lru[k]<least_time) {
600  least_time=kernel_cache.lru[k];
601  least_elem=k;
602  }
603  }
604  }
605 
606  if(least_elem != -1) {
607  kernel_cache_free(least_elem);
608  kernel_cache.index[kernel_cache.invindex[least_elem]]=-1;
609  kernel_cache.invindex[least_elem]=-1;
610  return(1);
611  }
612  return(0);
613 }
614 
615 // Get a free cache entry. In case cache is full, the lru
616 // element is removed.
617 KERNELCACHE_ELEM* CKernel::kernel_cache_clean_and_malloc(int32_t cacheidx)
618 {
619  int32_t result;
620  if((result = kernel_cache_malloc()) == -1) {
621  if(kernel_cache_free_lru()) {
622  result = kernel_cache_malloc();
623  }
624  }
625  kernel_cache.index[cacheidx]=result;
626  if(result == -1) {
627  return(0);
628  }
629  kernel_cache.invindex[result]=cacheidx;
630  kernel_cache.lru[kernel_cache.index[cacheidx]]=kernel_cache.time; // lru
631  return &kernel_cache.buffer[((KERNELCACHE_IDX) kernel_cache.activenum)*kernel_cache.index[cacheidx]];
632 }
633 #endif //USE_SVMLIGHT
634 
635 void CKernel::load(CFile* loader)
636 {
639 }
640 
641 void CKernel::save(CFile* writer)
642 {
643  SGMatrix<float64_t> k_matrix=get_kernel_matrix<float64_t>();
645  writer->set_matrix(k_matrix.matrix, k_matrix.num_rows, k_matrix.num_cols);
647 }
648 
650 {
651  SG_DEBUG("entering CKernel::remove_lhs_and_rhs\n")
652  if (rhs!=lhs)
653  SG_UNREF(rhs);
654  rhs = NULL;
655  num_rhs=0;
656 
657  SG_UNREF(lhs);
658  lhs = NULL;
659  num_lhs=0;
660  lhs_equals_rhs=false;
661 
662 #ifdef USE_SVMLIGHT
663  cache_reset();
664 #endif //USE_SVMLIGHT
665  SG_DEBUG("leaving CKernel::remove_lhs_and_rhs\n")
666 }
667 
669 {
670  if (rhs==lhs)
671  rhs=NULL;
672  SG_UNREF(lhs);
673  lhs = NULL;
674  num_lhs=0;
675  lhs_equals_rhs=false;
676 #ifdef USE_SVMLIGHT
677  cache_reset();
678 #endif //USE_SVMLIGHT
679 }
680 
683 {
684  if (rhs!=lhs)
685  SG_UNREF(rhs);
686  rhs = NULL;
687  num_rhs=0;
688  lhs_equals_rhs=false;
689 
690 #ifdef USE_SVMLIGHT
691  cache_reset();
692 #endif //USE_SVMLIGHT
693 }
694 
695 #define ENUM_CASE(n) case n: SG_INFO(#n " ") break;
696 
698 {
699  SG_INFO("%p - \"%s\" weight=%1.2f OPT:%s", this, get_name(),
701  get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" :
702  "SLOWBUTMEMEFFICIENT");
703 
704  switch (get_kernel_type())
705  {
766  }
767 
768  switch (get_feature_class())
769  {
780  ENUM_CASE(C_WD)
791  }
792 
793  switch (get_feature_type())
794  {
809  }
810  SG_INFO("\n")
811 }
812 #undef ENUM_CASE
813 
815  int32_t count, int32_t *IDX, float64_t * weights)
816 {
817  SG_ERROR("kernel does not support linadd optimization\n")
818  return false ;
819 }
820 
822 {
823  SG_ERROR("kernel does not support linadd optimization\n")
824  return false;
825 }
826 
828 {
829  SG_ERROR("kernel does not support linadd optimization\n")
830  return 0;
831 }
832 
834  int32_t num_vec, int32_t* vec_idx, float64_t* target, int32_t num_suppvec,
835  int32_t* IDX, float64_t* weights, float64_t factor)
836 {
837  SG_ERROR("kernel does not support batch computation\n")
838 }
839 
840 void CKernel::add_to_normal(int32_t vector_idx, float64_t weight)
841 {
842  SG_ERROR("kernel does not support linadd optimization, add_to_normal not implemented\n")
843 }
844 
846 {
847  SG_ERROR("kernel does not support linadd optimization, clear_normal not implemented\n")
848 }
849 
851 {
852  return 1;
853 }
854 
856  int32_t vector_idx, float64_t * subkernel_contrib)
857 {
858  SG_ERROR("kernel compute_by_subkernel not implemented\n")
859 }
860 
861 const float64_t* CKernel::get_subkernel_weights(int32_t &num_weights)
862 {
863  num_weights=1 ;
864  return &combined_kernel_weight ;
865 }
866 
868 {
869  int num_weights = 1;
870  const float64_t* weight = get_subkernel_weights(num_weights);
871  return SGVector<float64_t>(const_cast<float64_t*>(weight),1,false);
872 }
873 
875 {
876  ASSERT(weights.vector)
877  if (weights.vlen!=1)
878  SG_ERROR("number of subkernel weights should be one ...\n")
879 
880  combined_kernel_weight = weights.vector[0] ;
881 }
882 
884 {
885  if (kernel)
886  {
887  CKernel* casted=dynamic_cast<CKernel*>(kernel);
888  REQUIRE(casted, "CKernel::obtain_from_generic(): Error, provided object"
889  " of class \"%s\" is not a subclass of CKernel!\n",
890  kernel->get_name());
891  return casted;
892  }
893  else
894  return NULL;
895 }
896 
898 {
899  int32_t num_suppvec=svm->get_num_support_vectors();
900  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
901  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
902 
903  for (int32_t i=0; i<num_suppvec; i++)
904  {
905  sv_idx[i] = svm->get_support_vector(i);
906  sv_weight[i] = svm->get_alpha(i);
907  }
908  bool ret = init_optimization(num_suppvec, sv_idx, sv_weight);
909 
910  SG_FREE(sv_idx);
911  SG_FREE(sv_weight);
912  return ret;
913 }
914 
916 {
918  if (lhs_equals_rhs)
919  rhs=lhs;
920 }
921 
923 {
925 
926  if (lhs_equals_rhs)
927  rhs=NULL;
928 }
929 
931 {
933 
934  if (lhs_equals_rhs)
935  rhs=lhs;
936 }
937 
939  SG_ADD(&cache_size, "cache_size",
940  "Cache size in MB.", MS_NOT_AVAILABLE);
941  SG_ADD((CSGObject**) &lhs, "lhs",
942  "Feature vectors to occur on left hand side.", MS_NOT_AVAILABLE);
943  SG_ADD((CSGObject**) &rhs, "rhs",
944  "Feature vectors to occur on right hand side.", MS_NOT_AVAILABLE);
945  SG_ADD(&lhs_equals_rhs, "lhs_equals_rhs",
946  "If features on lhs are the same as on rhs.", MS_NOT_AVAILABLE);
947  SG_ADD(&num_lhs, "num_lhs", "Number of feature vectors on left hand side.",
949  SG_ADD(&num_rhs, "num_rhs", "Number of feature vectors on right hand side.",
951  SG_ADD(&combined_kernel_weight, "combined_kernel_weight",
952  "Combined kernel weight.", MS_AVAILABLE);
953  SG_ADD(&optimization_initialized, "optimization_initialized",
954  "Optimization is initialized.", MS_NOT_AVAILABLE);
955  SG_ADD((machine_int_t*) &opt_type, "opt_type",
956  "Optimization type.", MS_NOT_AVAILABLE);
957  SG_ADD(&properties, "properties", "Kernel properties.", MS_NOT_AVAILABLE);
958  SG_ADD((CSGObject**) &normalizer, "normalizer", "Normalize the kernel.",
959  MS_AVAILABLE);
960 }
961 
962 
963 void CKernel::init()
964 {
965  cache_size=10;
966  kernel_matrix=NULL;
967  lhs=NULL;
968  rhs=NULL;
969  num_lhs=0;
970  num_rhs=0;
971  lhs_equals_rhs=false;
976  normalizer=NULL;
977 
978 #ifdef USE_SVMLIGHT
979  memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
980 #endif //USE_SVMLIGHT
981 
983 }
984 
985 namespace shogun
986 {
988 template <class T> struct K_THREAD_PARAM
989 {
993  int32_t start;
995  int32_t end;
997  int64_t total_start;
999  int64_t total_end;
1001  int32_t m;
1003  int32_t n;
1009  bool verbose;
1010 };
1011 }
1012 
1014  bool no_diag)
1015 {
1016  SG_DEBUG("Entering\n");
1017 
1018  REQUIRE(has_features(), "No features assigned to kernel\n")
1019  REQUIRE(lhs_equals_rhs, "The kernel matrix is not symmetric!\n")
1020  REQUIRE(block_begin>=0 && block_begin<num_rhs,
1021  "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1022  REQUIRE(block_begin+block_size<=num_rhs,
1023  "Invalid block size (%d) at starting index (%d, %d)! "
1024  "Please use smaller blocks!", block_size, block_begin, block_begin)
1025  REQUIRE(block_size>=1, "Invalid block size (%d)!\n", block_size)
1026 
1027  float64_t sum=0.0;
1028 
1029  // since the block is symmetric with main diagonal inside, we can save half
1030  // the computation with using only the upper triangular part.
1031  // this can be done in parallel
1032 #pragma omp parallel for
1033  for (index_t i=0; i<block_size; ++i)
1034  {
1035  // compute the kernel values on the upper triangular part of the kernel
1036  // matrix and compute sum on the fly
1037  for (index_t j=i+1; j<block_size; ++j)
1038  {
1039  float64_t k=kernel(i+block_begin, j+block_begin);
1040 #pragma omp atomic
1041  sum+=k;
1042  }
1043  }
1044 
1045  // the actual sum would be twice of what we computed
1046  sum*=2;
1047 
1048  // add the diagonal elements if required - keeping this check
1049  // outside of the loop to save cycles
1050  if (!no_diag)
1051  {
1052 #pragma omp parallel for
1053  for (index_t i=0; i<block_size; ++i)
1054  {
1055  float64_t diag=kernel(i+block_begin, i+block_begin);
1056 #pragma omp atomic
1057  sum+=diag;
1058  }
1059  }
1060 
1061  SG_DEBUG("Leaving\n");
1062 
1063  return sum;
1064 }
1065 
1066 float64_t CKernel::sum_block(index_t block_begin_row, index_t block_begin_col,
1067  index_t block_size_row, index_t block_size_col, bool no_diag)
1068 {
1069  SG_DEBUG("Entering\n");
1070 
1071  REQUIRE(has_features(), "No features assigned to kernel\n")
1072  REQUIRE(block_begin_row>=0 && block_begin_row<num_lhs &&
1073  block_begin_col>=0 && block_begin_col<num_rhs,
1074  "Invalid block begin index (%d, %d)!\n",
1075  block_begin_row, block_begin_col)
1076  REQUIRE(block_begin_row+block_size_row<=num_lhs &&
1077  block_begin_col+block_size_col<=num_rhs,
1078  "Invalid block size (%d, %d) at starting index (%d, %d)! "
1079  "Please use smaller blocks!", block_size_row, block_size_col,
1080  block_begin_row, block_begin_col)
1081  REQUIRE(block_size_row>=1 && block_size_col>=1,
1082  "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
1083 
1084  // check if removal of diagonal is required/valid
1085  if (no_diag && block_size_row!=block_size_col)
1086  {
1087  SG_WARNING("Not removing the main diagonal since block is not square!\n");
1088  no_diag=false;
1089  }
1090 
1091  float64_t sum=0.0;
1092 
1093  // this can be done in parallel for the rows/cols
1094 #pragma omp parallel for
1095  for (index_t i=0; i<block_size_row; ++i)
1096  {
1097  // compute the kernel values and compute sum on the fly
1098  for (index_t j=0; j<block_size_col; ++j)
1099  {
1100  float64_t k=no_diag && i==j ? 0 :
1101  kernel(i+block_begin_row, j+block_begin_col);
1102 #pragma omp atomic
1103  sum+=k;
1104  }
1105  }
1106 
1107  SG_DEBUG("Leaving\n");
1108 
1109  return sum;
1110 }
1111 
1113  index_t block_size, bool no_diag)
1114 {
1115  SG_DEBUG("Entering\n");
1116 
1117  REQUIRE(has_features(), "No features assigned to kernel\n")
1118  REQUIRE(lhs_equals_rhs, "The kernel matrix is not symmetric!\n")
1119  REQUIRE(block_begin>=0 && block_begin<num_rhs,
1120  "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1121  REQUIRE(block_begin+block_size<=num_rhs,
1122  "Invalid block size (%d) at starting index (%d, %d)! "
1123  "Please use smaller blocks!", block_size, block_begin, block_begin)
1124  REQUIRE(block_size>=1, "Invalid block size (%d)!\n", block_size)
1125 
1126  // initialize the vector that accumulates the row/col-wise sum on the go
1127  SGVector<float64_t> row_sum(block_size);
1128  row_sum.set_const(0.0);
1129 
1130  // since the block is symmetric with main diagonal inside, we can save half
1131  // the computation with using only the upper triangular part.
1132  // this can be done in parallel for the rows/cols
1133 #pragma omp parallel for
1134  for (index_t i=0; i<block_size; ++i)
1135  {
1136  // compute the kernel values on the upper triangular part of the kernel
1137  // matrix and compute row-wise sum on the fly
1138  for (index_t j=i+1; j<block_size; ++j)
1139  {
1140  float64_t k=kernel(i+block_begin, j+block_begin);
1141 #pragma omp critical
1142  {
1143  row_sum[i]+=k;
1144  row_sum[j]+=k;
1145  }
1146  }
1147  }
1148 
1149  // add the diagonal elements if required - keeping this check
1150  // outside of the loop to save cycles
1151  if (!no_diag)
1152  {
1153 #pragma omp parallel for
1154  for (index_t i=0; i<block_size; ++i)
1155  {
1156  float64_t diag=kernel(i+block_begin, i+block_begin);
1157  row_sum[i]+=diag;
1158  }
1159  }
1160 
1161  SG_DEBUG("Leaving\n");
1162 
1163  return row_sum;
1164 }
1165 
1167  block_begin, index_t block_size, bool no_diag)
1168 {
1169  SG_DEBUG("Entering\n");
1170 
1171  REQUIRE(has_features(), "No features assigned to kernel\n")
1172  REQUIRE(lhs_equals_rhs, "The kernel matrix is not symmetric!\n")
1173  REQUIRE(block_begin>=0 && block_begin<num_rhs,
1174  "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1175  REQUIRE(block_begin+block_size<=num_rhs,
1176  "Invalid block size (%d) at starting index (%d, %d)! "
1177  "Please use smaller blocks!", block_size, block_begin, block_begin)
1178  REQUIRE(block_size>=1, "Invalid block size (%d)!\n", block_size)
1179 
1180  // initialize the matrix that accumulates the row/col-wise sum on the go
1181  // the first column stores the sum of kernel values
1182  // the second column stores the sum of squared kernel values
1183  SGMatrix<float64_t> row_sum(block_size, 2);
1184  row_sum.set_const(0.0);
1185 
1186  // since the block is symmetric with main diagonal inside, we can save half
1187  // the computation with using only the upper triangular part
1188  // this can be done in parallel for the rows/cols
1189 #pragma omp parallel for
1190  for (index_t i=0; i<block_size; ++i)
1191  {
1192  // compute the kernel values on the upper triangular part of the kernel
1193  // matrix and compute row-wise sum and squared sum on the fly
1194  for (index_t j=i+1; j<block_size; ++j)
1195  {
1196  float64_t k=kernel(i+block_begin, j+block_begin);
1197 #pragma omp critical
1198  {
1199  row_sum(i, 0)+=k;
1200  row_sum(j, 0)+=k;
1201  row_sum(i, 1)+=k*k;
1202  row_sum(j, 1)+=k*k;
1203  }
1204  }
1205  }
1206 
1207  // add the diagonal elements if required - keeping this check
1208  // outside of the loop to save cycles
1209  if (!no_diag)
1210  {
1211 #pragma omp parallel for
1212  for (index_t i=0; i<block_size; ++i)
1213  {
1214  float64_t diag=kernel(i+block_begin, i+block_begin);
1215  row_sum(i, 0)+=diag;
1216  row_sum(i, 1)+=diag*diag;
1217  }
1218  }
1219 
1220  SG_DEBUG("Leaving\n");
1221 
1222  return row_sum;
1223 }
1224 
1226  index_t block_begin_col, index_t block_size_row,
1227  index_t block_size_col, bool no_diag)
1228 {
1229  SG_DEBUG("Entering\n");
1230 
1231  REQUIRE(has_features(), "No features assigned to kernel\n")
1232  REQUIRE(block_begin_row>=0 && block_begin_row<num_lhs &&
1233  block_begin_col>=0 && block_begin_col<num_rhs,
1234  "Invalid block begin index (%d, %d)!\n",
1235  block_begin_row, block_begin_col)
1236  REQUIRE(block_begin_row+block_size_row<=num_lhs &&
1237  block_begin_col+block_size_col<=num_rhs,
1238  "Invalid block size (%d, %d) at starting index (%d, %d)! "
1239  "Please use smaller blocks!", block_size_row, block_size_col,
1240  block_begin_row, block_begin_col)
1241  REQUIRE(block_size_row>=1 && block_size_col>=1,
1242  "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
1243 
1244  // check if removal of diagonal is required/valid
1245  if (no_diag && block_size_row!=block_size_col)
1246  {
1247  SG_WARNING("Not removing the main diagonal since block is not square!\n");
1248  no_diag=false;
1249  }
1250 
1251  // initialize the vector that accumulates the row/col-wise sum on the go
1252  // the first block_size_row entries store the row-wise sum of kernel values
1253  // the nextt block_size_col entries store the col-wise sum of kernel values
1254  SGVector<float64_t> sum(block_size_row+block_size_col);
1255  sum.set_const(0.0);
1256 
1257  // this can be done in parallel for the rows/cols
1258 #pragma omp parallel for
1259  for (index_t i=0; i<block_size_row; ++i)
1260  {
1261  // compute the kernel values and compute sum on the fly
1262  for (index_t j=0; j<block_size_col; ++j)
1263  {
1264  float64_t k=no_diag && i==j ? 0 :
1265  kernel(i+block_begin_row, j+block_begin_col);
1266 #pragma omp critical
1267  {
1268  sum[i]+=k;
1269  sum[j+block_size_row]+=k;
1270  }
1271  }
1272  }
1273 
1274  SG_DEBUG("Leaving\n");
1275 
1276  return sum;
1277 }
1278 
1279 template <class T> void* CKernel::get_kernel_matrix_helper(void* p)
1280 {
1281  K_THREAD_PARAM<T>* params= (K_THREAD_PARAM<T>*) p;
1282  int32_t i_start=params->start;
1283  int32_t i_end=params->end;
1284  CKernel* k=params->kernel;
1285  T* result=params->result;
1286  bool symmetric=params->symmetric;
1287  int32_t n=params->n;
1288  int32_t m=params->m;
1289  bool verbose=params->verbose;
1290  int64_t total_start=params->total_start;
1291  int64_t total_end=params->total_end;
1292  int64_t total=total_start;
1293 
1294  for (int32_t i=i_start; i<i_end; i++)
1295  {
1296  int32_t j_start=0;
1297 
1298  if (symmetric)
1299  j_start=i;
1300 
1301  for (int32_t j=j_start; j<n; j++)
1302  {
1303  float64_t v=k->kernel(i,j);
1304  result[i+j*m]=v;
1305 
1306  if (symmetric && i!=j)
1307  result[j+i*m]=v;
1308 
1309  if (verbose)
1310  {
1311  total++;
1312 
1313  if (symmetric && i!=j)
1314  total++;
1315 
1316  if (total%100 == 0)
1317  SG_OBJ_PROGRESS(k, total, total_start, total_end)
1318 
1320  break;
1321  }
1322  }
1323 
1324  }
1325 
1326  return NULL;
1327 }
1328 
1329 template <class T>
1331 {
1332  T* result = NULL;
1333 
1334  REQUIRE(has_features(), "no features assigned to kernel\n")
1335 
1336  int32_t m=get_num_vec_lhs();
1337  int32_t n=get_num_vec_rhs();
1338 
1339  int64_t total_num = int64_t(m)*n;
1340 
1341  // if lhs == rhs and sizes match assume k(i,j)=k(j,i)
1342  bool symmetric= (lhs && lhs==rhs && m==n);
1343 
1344  SG_DEBUG("returning kernel matrix of size %dx%d\n", m, n)
1345 
1346  result=SG_MALLOC(T, total_num);
1347 
1348  int32_t num_threads=parallel->get_num_threads();
1349  if (num_threads < 2)
1350  {
1351  K_THREAD_PARAM<T> params;
1352  params.kernel=this;
1353  params.result=result;
1354  params.start=0;
1355  params.end=m;
1356  params.total_start=0;
1357  params.total_end=total_num;
1358  params.n=n;
1359  params.m=m;
1360  params.symmetric=symmetric;
1361  params.verbose=true;
1362  get_kernel_matrix_helper<T>((void*) &params);
1363  }
1364  else
1365  {
1366  pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
1367  K_THREAD_PARAM<T>* params = SG_MALLOC(K_THREAD_PARAM<T>, num_threads);
1368  int64_t step= total_num/num_threads;
1369 
1370  int32_t t;
1371 
1372  num_threads--;
1373  for (t=0; t<num_threads; t++)
1374  {
1375  params[t].kernel = this;
1376  params[t].result = result;
1377  params[t].start = compute_row_start(t*step, n, symmetric);
1378  params[t].end = compute_row_start((t+1)*step, n, symmetric);
1379  params[t].total_start=t*step;
1380  params[t].total_end=(t+1)*step;
1381  params[t].n=n;
1382  params[t].m=m;
1383  params[t].symmetric=symmetric;
1384  params[t].verbose=false;
1385 
1386  int code=pthread_create(&threads[t], NULL,
1387  CKernel::get_kernel_matrix_helper<T>, (void*)&params[t]);
1388 
1389  if (code != 0)
1390  {
1391  SG_WARNING("Thread creation failed (thread %d of %d) "
1392  "with error:'%s'\n",t, num_threads, strerror(code));
1393  num_threads=t;
1394  break;
1395  }
1396  }
1397 
1398  params[t].kernel = this;
1399  params[t].result = result;
1400  params[t].start = compute_row_start(t*step, n, symmetric);
1401  params[t].end = m;
1402  params[t].total_start=t*step;
1403  params[t].total_end=total_num;
1404  params[t].n=n;
1405  params[t].m=m;
1406  params[t].symmetric=symmetric;
1407  params[t].verbose=true;
1408  get_kernel_matrix_helper<T>(&params[t]);
1409 
1410  for (t=0; t<num_threads; t++)
1411  {
1412  if (pthread_join(threads[t], NULL) != 0)
1413  SG_WARNING("pthread_join of thread %d/%d failed\n", t, num_threads)
1414  }
1415 
1416  SG_FREE(params);
1417  SG_FREE(threads);
1418  }
1419 
1420  SG_DONE()
1421 
1422  return SGMatrix<T>(result,m,n,true);
1423 }
1424 
1425 
1428 
1429 template void* CKernel::get_kernel_matrix_helper<float64_t>(void* p);
1430 template void* CKernel::get_kernel_matrix_helper<float32_t>(void* p);
1431 

SHOGUN Machine Learning Toolbox - Documentation