39 using namespace shogun;
76 SG_ERROR(
"Kernel still initialized on destruction.\n");
81 SG_INFO(
"Kernel deleted (%p).\n",
this);
174 SG_ERROR(
"kernel has zero rows: num_lhs=%d num_rhs=%d\n",
177 uint64_t buffer_size=0;
185 if (buffer_size>((uint64_t) totdoc)*totdoc)
186 buffer_size=((uint64_t) totdoc)*totdoc;
208 for(i=0;i<totdoc;i++) {
212 for(i=0;i<totdoc;i++) {
218 for(i=0;i<totdoc;i++) {
227 int32_t docnum, int32_t *active2dnum,
float64_t *buffer,
bool full_line)
233 if (docnum>=num_vectors)
234 docnum=2*num_vectors-1-docnum;
254 for(i=0;(j=active2dnum[i])>=0;i++)
277 for(i=0;(j=active2dnum[i])>=0;i++)
292 register int32_t j,k,l;
302 cache = kernel_cache_clean_and_malloc(m);
310 if((
kernel_cache.index[k] != -1) && (l != -1) && (k != m)) {
324 perror(
"Error: Kernel cache full! => increase cache size");
329 void* CKernel::cache_multiple_kernel_row_helper(
void* p)
332 S_KTHREAD_PARAM* params = (S_KTHREAD_PARAM*) p;
334 for (int32_t i=params->start; i<params->end; i++)
337 int32_t m = params->uncached_rows[i];
338 l=params->kernel_cache->totdoc2active[m];
340 for(j=0;j<params->kernel_cache->activenum;j++)
342 k=params->kernel_cache->active2totdoc[j];
344 if((params->kernel_cache->index[k] != -1) && (l != -1) && (!params->needs_computation[k])) {
345 cache[j]=params->kernel_cache->buffer[((
KERNELCACHE_IDX) params->kernel_cache->activenum)
346 *params->kernel_cache->index[k]+l];
350 if (k>=params->num_vectors)
351 k=2*params->num_vectors-1-k;
353 cache[j]=params->kernel->kernel(m, k);
358 params->needs_computation[m]=0;
372 for(int32_t i=0;i<num_rows;i++)
379 int32_t* uncached_rows =
SG_MALLOC(int32_t, num_rows);
381 pthread_t* threads =
SG_MALLOC(pthread_t, nthreads-1);
382 S_KTHREAD_PARAM* params =
SG_MALLOC(S_KTHREAD_PARAM, nthreads-1);
383 int32_t num_threads=nthreads-1;
386 uint8_t* needs_computation=
SG_CALLOC(uint8_t, num_vec);
393 for (int32_t i=0; i<num_rows; i++)
402 needs_computation[idx]=1;
403 uncached_rows[num]=idx;
404 cache[num]= kernel_cache_clean_and_malloc(idx);
407 SG_ERROR(
"Kernel cache full! => increase cache size\n");
422 for (int32_t t=0; t<num_threads; t++)
424 params[t].kernel =
this;
426 params[t].cache = cache;
427 params[t].uncached_rows = uncached_rows;
428 params[t].needs_computation = needs_computation;
429 params[t].num_uncached = num;
430 params[t].start = t*step;
431 params[t].end = (t+1)*step;
435 int code=pthread_create(&threads[t], NULL,
436 CKernel::cache_multiple_kernel_row_helper, (
void*)¶ms[t]);
440 SG_WARNING(
"Thread creation failed (thread %d of %d) "
441 "with error:'%s'\n",t, num_threads, strerror(code));
452 S_KTHREAD_PARAM last_param;
453 last_param.kernel =
this;
455 last_param.cache = cache;
456 last_param.uncached_rows = uncached_rows;
457 last_param.needs_computation = needs_computation;
458 last_param.start = end;
459 last_param.num_uncached = num;
460 last_param.end = num;
463 cache_multiple_kernel_row_helper(&last_param);
466 for (int32_t t=0; t<num_threads; t++)
468 if (pthread_join(threads[t], NULL) != 0)
469 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads);
484 int32_t totdoc, int32_t numshrink, int32_t *after)
486 register int32_t i,j,jj,scount;
491 for(j=0;j<totdoc;j++) {
495 for(jj=0;(jj<
kernel_cache.activenum) && (scount<numshrink);jj++) {
518 for(j=0;j<totdoc;j++) {
519 if((keep[j]) && (
kernel_cache.totdoc2active[j] != -1)) {
564 int32_t CKernel::kernel_cache_malloc()
580 void CKernel::kernel_cache_free(int32_t cacheidx)
588 int32_t CKernel::kernel_cache_free_lru()
590 register int32_t k,least_elem=-1,least_time;
602 if(least_elem != -1) {
603 kernel_cache_free(least_elem);
616 if((result = kernel_cache_malloc()) == -1) {
617 if(kernel_cache_free_lru()) {
618 result = kernel_cache_malloc();
629 #endif //USE_SVMLIGHT
659 #endif //USE_SVMLIGHT
672 #endif //USE_SVMLIGHT
686 #endif //USE_SVMLIGHT
689 #define ENUM_CASE(n) case n: SG_INFO(#n " "); break;
696 "SLOWBUTMEMEFFICIENT");
804 int32_t count, int32_t *IDX,
float64_t * weights)
806 SG_ERROR(
"kernel does not support linadd optimization\n");
812 SG_ERROR(
"kernel does not support linadd optimization\n");
818 SG_ERROR(
"kernel does not support linadd optimization\n");
823 int32_t num_vec, int32_t* vec_idx,
float64_t* target, int32_t num_suppvec,
826 SG_ERROR(
"kernel does not support batch computation\n");
831 SG_ERROR(
"kernel does not support linadd optimization, add_to_normal not implemented\n");
836 SG_ERROR(
"kernel does not support linadd optimization, clear_normal not implemented\n");
845 int32_t vector_idx,
float64_t * subkernel_contrib)
847 SG_ERROR(
"kernel compute_by_subkernel not implemented\n");
860 SG_ERROR(
"number of subkernel weights should be one ...\n");
868 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
871 for (int32_t i=0; i<num_suppvec; i++)
915 SG_ADD(&
num_lhs,
"num_lhs",
"Number of feature vectors on left hand side.",
917 SG_ADD(&
num_rhs,
"num_rhs",
"Number of feature vectors on right hand side.",
947 #endif //USE_SVMLIGHT