39 using namespace shogun;
76 SG_ERROR(
"Kernel still initialized on destruction.\n")
81 SG_INFO(
"Kernel deleted (%p).\n",
this)
100 SG_DEBUG(
"entering CKernel::init(%p, %p)\n", l, r)
107 REQUIRE(l,
"CKernel::init(%p, %p): LHS features required!\n", l, r)
108 REQUIRE(r,
"CKernel::init(%p, %p): RHS features required!\n", l, r)
137 SG_DEBUG(
"leaving CKernel::init(%p, %p)\n", l, r)
177 SG_ERROR(
"kernel has zero rows: num_lhs=%d num_rhs=%d\n",
180 uint64_t buffer_size=0;
188 if (buffer_size>((uint64_t) totdoc)*totdoc)
189 buffer_size=((uint64_t) totdoc)*totdoc;
200 kernel_cache.active2totdoc = SG_MALLOC(int32_t, totdoc);
201 kernel_cache.totdoc2active = SG_MALLOC(int32_t, totdoc);
211 for(i=0;i<totdoc;i++) {
215 for(i=0;i<totdoc;i++) {
221 for(i=0;i<totdoc;i++) {
230 int32_t docnum, int32_t *active2dnum,
float64_t *buffer,
bool full_line)
236 if (docnum>=num_vectors)
237 docnum=2*num_vectors-1-docnum;
257 for(i=0;(j=active2dnum[i])>=0;i++)
280 for(i=0;(j=active2dnum[i])>=0;i++)
295 register int32_t j,k,l;
305 cache = kernel_cache_clean_and_malloc(m);
313 if((
kernel_cache.index[k] != -1) && (l != -1) && (k != m)) {
327 perror(
"Error: Kernel cache full! => increase cache size");
332 void* CKernel::cache_multiple_kernel_row_helper(
void* p)
335 S_KTHREAD_PARAM* params = (S_KTHREAD_PARAM*) p;
337 for (int32_t i=params->start; i<params->end; i++)
340 int32_t m = params->uncached_rows[i];
341 l=params->kernel_cache->totdoc2active[m];
343 for(j=0;j<params->kernel_cache->activenum;j++)
345 k=params->kernel_cache->active2totdoc[j];
347 if((params->kernel_cache->index[k] != -1) && (l != -1) && (!params->needs_computation[k])) {
348 cache[j]=params->kernel_cache->buffer[((
KERNELCACHE_IDX) params->kernel_cache->activenum)
349 *params->kernel_cache->index[k]+l];
353 if (k>=params->num_vectors)
354 k=2*params->num_vectors-1-k;
356 cache[j]=params->kernel->kernel(m, k);
361 params->needs_computation[m]=0;
375 for(int32_t i=0;i<num_rows;i++)
382 int32_t* uncached_rows = SG_MALLOC(int32_t, num_rows);
384 pthread_t* threads = SG_MALLOC(pthread_t, nthreads-1);
385 S_KTHREAD_PARAM* params = SG_MALLOC(S_KTHREAD_PARAM, nthreads-1);
386 int32_t num_threads=nthreads-1;
389 uint8_t* needs_computation=SG_CALLOC(uint8_t, num_vec);
396 for (int32_t i=0; i<num_rows; i++)
405 needs_computation[idx]=1;
406 uncached_rows[num]=idx;
407 cache[num]= kernel_cache_clean_and_malloc(idx);
410 SG_ERROR(
"Kernel cache full! => increase cache size\n")
425 for (int32_t t=0; t<num_threads; t++)
427 params[t].kernel =
this;
429 params[t].cache = cache;
430 params[t].uncached_rows = uncached_rows;
431 params[t].needs_computation = needs_computation;
432 params[t].num_uncached = num;
433 params[t].start = t*step;
434 params[t].end = (t+1)*step;
438 int code=pthread_create(&threads[t], NULL,
439 CKernel::cache_multiple_kernel_row_helper, (
void*)¶ms[t]);
443 SG_WARNING(
"Thread creation failed (thread %d of %d) "
444 "with error:'%s'\n",t, num_threads, strerror(code));
455 S_KTHREAD_PARAM last_param;
456 last_param.kernel =
this;
458 last_param.cache = cache;
459 last_param.uncached_rows = uncached_rows;
460 last_param.needs_computation = needs_computation;
461 last_param.start = end;
462 last_param.num_uncached = num;
463 last_param.end = num;
466 cache_multiple_kernel_row_helper(&last_param);
469 for (int32_t t=0; t<num_threads; t++)
471 if (pthread_join(threads[t], NULL) != 0)
472 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads)
475 SG_FREE(needs_computation);
479 SG_FREE(uncached_rows);
487 int32_t totdoc, int32_t numshrink, int32_t *after)
490 register int32_t i,j,jj,scount;
494 keep=SG_MALLOC(int32_t, totdoc);
495 for(j=0;j<totdoc;j++) {
499 for(jj=0;(jj<
kernel_cache.activenum) && (scount<numshrink);jj++) {
522 for(j=0;j<totdoc;j++) {
523 if((keep[j]) && (
kernel_cache.totdoc2active[j] != -1)) {
568 int32_t CKernel::kernel_cache_malloc()
584 void CKernel::kernel_cache_free(int32_t cacheidx)
592 int32_t CKernel::kernel_cache_free_lru()
594 register int32_t k,least_elem=-1,least_time;
606 if(least_elem != -1) {
607 kernel_cache_free(least_elem);
620 if((result = kernel_cache_malloc()) == -1) {
621 if(kernel_cache_free_lru()) {
622 result = kernel_cache_malloc();
633 #endif //USE_SVMLIGHT
651 SG_DEBUG(
"entering CKernel::remove_lhs_and_rhs\n")
664 #endif //USE_SVMLIGHT
665 SG_DEBUG(
"leaving CKernel::remove_lhs_and_rhs\n")
678 #endif //USE_SVMLIGHT
692 #endif //USE_SVMLIGHT
695 #define ENUM_CASE(n) case n: SG_INFO(#n " ") break;
702 "SLOWBUTMEMEFFICIENT");
813 int32_t count, int32_t *IDX,
float64_t * weights)
815 SG_ERROR(
"kernel does not support linadd optimization\n")
821 SG_ERROR(
"kernel does not support linadd optimization\n")
827 SG_ERROR(
"kernel does not support linadd optimization\n")
832 int32_t num_vec, int32_t* vec_idx,
float64_t* target, int32_t num_suppvec,
835 SG_ERROR(
"kernel does not support batch computation\n")
840 SG_ERROR(
"kernel does not support linadd optimization, add_to_normal not implemented\n")
845 SG_ERROR(
"kernel does not support linadd optimization, clear_normal not implemented\n")
854 int32_t vector_idx,
float64_t * subkernel_contrib)
856 SG_ERROR(
"kernel compute_by_subkernel not implemented\n")
876 SG_ERROR(
"number of subkernel weights should be one ...\n")
886 REQUIRE(casted,
"CKernel::obtain_from_generic(): Error, provided object"
887 " of class \"%s\" is not a subclass of CKernel!\n",
898 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
901 for (int32_t i=0; i<num_suppvec; i++)
945 SG_ADD(&
num_lhs,
"num_lhs",
"Number of feature vectors on left hand side.",
947 SG_ADD(&
num_rhs,
"num_rhs",
"Number of feature vectors on right hand side.",
978 #endif //USE_SVMLIGHT
1014 int32_t i_start=params->
start;
1015 int32_t i_end=params->
end;
1017 T* result=params->
result;
1019 int32_t n=params->
n;
1020 int32_t m=params->
m;
1024 int64_t total=total_start;
1026 for (int32_t i=i_start; i<i_end; i++)
1033 for (int32_t j=j_start; j<n; j++)
1038 if (symmetric && i!=j)
1045 if (symmetric && i!=j)
1071 int64_t total_num = int64_t(m)*n;
1074 bool symmetric= (
lhs &&
lhs==
rhs && m==n);
1078 result=SG_MALLOC(T, total_num);
1080 int32_t num_threads=
parallel->get_num_threads();
1081 if (num_threads < 2)
1094 get_kernel_matrix_helper<T>((
void*) ¶ms);
1098 pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
1100 int64_t step= total_num/num_threads;
1105 for (t=0; t<num_threads; t++)
1108 params[t].
result = result;
1118 int code=pthread_create(&threads[t], NULL,
1119 CKernel::get_kernel_matrix_helper<T>, (
void*)¶ms[t]);
1123 SG_WARNING(
"Thread creation failed (thread %d of %d) "
1124 "with error:'%s'\n",t, num_threads, strerror(code));
1131 params[t].
result = result;
1140 get_kernel_matrix_helper<T>(¶ms[t]);
1142 for (t=0; t<num_threads; t++)
1144 if (pthread_join(threads[t], NULL) != 0)
1145 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads)
1154 return
SGMatrix<T>(result,m,n,true);