<commit_before>/*
 * EXCEPT FOR THE KERNEL CACHING FUNCTIONS WHICH ARE (W) THORSTEN JOACHIMS
 * COPYRIGHT (C) 1999  UNIVERSITAET DORTMUND - ALL RIGHTS RESERVED
 *
 * this program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 1999-2007 <NAME>
 * Written (W) 1999-2007 <NAME>
 * Copyright (C) 1999-2007 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include "lib/config.h"

#include "lib/common.h"
#include "lib/io.h"
#include "lib/File.h"
#include "lib/Time.h"
#include "base/Parallel.h"

#include "kernel/Kernel.h"
#include "features/Features.h"

#include <string.h>
#include <unistd.h>
#include <math.h>

#ifndef WIN32
#include <pthread.h>
#endif

CKernel::CKernel(INT size)
	: CSGObject(), kernel_matrix(NULL), precomputed_matrix(NULL),
	precompute_subkernel_matrix(false), precompute_matrix(false), lhs(NULL),
	rhs(NULL), combined_kernel_weight(1), optimization_initialized(false),
	opt_type(FASTBUTMEMHUNGRY), properties(KP_NONE)
{
	if (size<10)
		size=10;

	cache_size=size;
#ifdef USE_SVMLIGHT
	memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
#endif //USE_SVMLIGHT
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");
}

		
CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, INT size)
  : CSGObject(), kernel_matrix(NULL), precomputed_matrix(NULL),
	precompute_subkernel_matrix(false), precompute_matrix(false), 
	lhs(NULL), rhs(NULL), combined_kernel_weight(1), optimization_initialized(false),
	opt_type(FASTBUTMEMHUNGRY), properties(KP_NONE)
{
	if (size<10)
		size=10;

	cache_size=size;
#ifdef USE_SVMLIGHT
	memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
#endif //USE_SVMLIGHT
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");

	init(p_lhs, p_rhs);
}

CKernel::~CKernel()
{
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");

    SG_UNREF(lhs);
    if (lhs!=rhs)
        SG_UNREF(rhs);

#ifdef USE_SVMLIGHT
	kernel_cache_cleanup();
#endif //USE_SVMLIGHT

	delete[] precomputed_matrix ;
	precomputed_matrix=NULL ;

	SG_INFO("Kernel deleted (%p)\n", this);
}

void CKernel::get_kernel_matrix(DREAL** dst, INT* m, INT* n)
{
	ASSERT(dst && m && n);

	DREAL* result = NULL;
	CFeatures* f1 = get_lhs();
	CFeatures* f2 = get_rhs();

	if (f1 && f2)
	{
		INT num_vec1=f1->get_num_vectors();
		INT num_vec2=f2->get_num_vectors();
		*m=num_vec1;
		*n=num_vec2;

		LONG total_num = num_vec1 * num_vec2;
		INT num_done = 0;
		SG_DEBUG( "returning kernel matrix of size %dx%d\n", num_vec1, num_vec2);

		result=new DREAL[total_num];
		ASSERT(result);

		if ( (f1 == f2) && (num_vec1 == num_vec2) )
		{
			for (INT i=0; i<num_vec1; i++)
			{
				for (INT j=i; j<num_vec1; j++)
				{
					double v=kernel(i,j);

					result[i+j*num_vec1]=v;
					result[j+i*num_vec1]=v;
<commit_msg>kernel: - Fixed a little issue with memory created with new (now malloc) when being   free'd (from the outside)<commit_after>/*
 * EXCEPT FOR THE KERNEL CACHING FUNCTIONS WHICH ARE (W) THORSTEN JOACHIMS
 * COPYRIGHT (C) 1999  UNIVERSITAET DORTMUND - ALL RIGHTS RESERVED
 *
 * this program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 1999-2007 <NAME>
 * Written (W) 1999-2007 <NAME>
 * Copyright (C) 1999-2007 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include "lib/config.h"

#include "lib/common.h"
#include "lib/io.h"
#include "lib/File.h"
#include "lib/Time.h"
#include "base/Parallel.h"

#include "kernel/Kernel.h"
#include "features/Features.h"

#include <string.h>
#include <unistd.h>
#include <math.h>

#ifndef WIN32
#include <pthread.h>
#endif

CKernel::CKernel(INT size)
	: CSGObject(), kernel_matrix(NULL), precomputed_matrix(NULL),
	precompute_subkernel_matrix(false), precompute_matrix(false), lhs(NULL),
	rhs(NULL), combined_kernel_weight(1), optimization_initialized(false),
	opt_type(FASTBUTMEMHUNGRY), properties(KP_NONE)
{
	if (size<10)
		size=10;

	cache_size=size;
#ifdef USE_SVMLIGHT
	memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
#endif //USE_SVMLIGHT
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");
}

		
CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, INT size)
  : CSGObject(), kernel_matrix(NULL), precomputed_matrix(NULL),
	precompute_subkernel_matrix(false), precompute_matrix(false), 
	lhs(NULL), rhs(NULL), combined_kernel_weight(1), optimization_initialized(false),
	opt_type(FASTBUTMEMHUNGRY), properties(KP_NONE)
{
	if (size<10)
		size=10;

	cache_size=size;
#ifdef USE_SVMLIGHT
	memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE));
#endif //USE_SVMLIGHT
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");

	init(p_lhs, p_rhs);
}

CKernel::~CKernel()
{
	if (get_is_initialized()) 
      SG_ERROR( "COptimizableKernel still initialized on destruction");

    SG_UNREF(lhs);
    if (lhs!=rhs)
        SG_UNREF(rhs);

#ifdef USE_SVMLIGHT
	kernel_cache_cleanup();
#endif //USE_SVMLIGHT

	delete[] precomputed_matrix ;
	precomputed_matrix=NULL ;

	SG_INFO("Kernel deleted (%p)\n", this);
}

void CKernel::get_kernel_matrix(DREAL** dst, INT* m, INT* n)
{
	ASSERT(dst && m && n);

	DREAL* result = NULL;
	CFeatures* f1 = get_lhs();
	CFeatures* f2 = get_rhs();

	if (f1 && f2)
	{
		INT num_vec1=f1->get_num_vectors();
		INT num_vec2=f2->get_num_vectors();
		*m=num_vec1;
		*n=num_vec2;

		LONG total_num = num_vec1 * num_vec2;
		INT num_done = 0;
		SG_DEBUG( "returning kernel matrix of size %dx%d\n", num_vec1, num_vec2);

		result=(DREAL *) malloc(sizeof(DREAL)*total_num);
		ASSERT(result);

		if ( (f1 == f2) && (num_vec1 == num_vec2) )
		{
			for (INT i=0; i<num_vec1; i++)
			{
				for (INT j=i; j<num_vec1; j++)
				{
					double v=kernel(i,j);

					result[i+j*num_vec1]=v;
					result[j+i*num_vec1]=v;

{'fraction_non_alphanumeric': 0.13637095475768798, 'fraction_numerical': 0.02221864434068588, 'mean_word_length': 4.990356798457087, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1420463', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2371, 'n_words': 547}