I need some help to make my code more efficient. I write a C++
program implementing the kmeans algorithm with parallelization
(using object oriented MPI).
My question is rather stupid but since I program in C# the last 2 years
and the garbage collector does the job for me... (I know I'm lame
)So I have 2 classes:
Code:
class TextTermEntry : public OOMPI_User_type
{
public:
//Members
int code;
int frequency;
float weight;
//Constructors
TextTermEntry();
TextTermEntry(const TextTermEntry& term);
private:
// Static variable to hold the newly constructed
// MPI_Datatype
static OOMPI_Datatype type;
};
class TextEntity : public OOMPI_User_type
{
public:
//Members
char file_path[FILEPATH_SIZE];
int text_terms_size;
TextTermEntry* text_terms;
//Constructors
TextEntity();
TextEntity(const TextEntity& text);
//Functions
void set_file_path(char* _file_path);
void initialize_text_terms(void);
void initialize_centroid_terms(int max_terms_size);
void initialize_centroid_terms();
void get_weight_vector(float* weight_vector, int vector_size);
bool recalculate_terms_weight(float* new_weight_vector, int vector_size, int text_count);
void delete_text_terms(void);
private:
// Static variable to hold the newly constructed
// MPI_Datatype
static OOMPI_Datatype type;
};
the following way:
Code:
void TextEntity::initialize_text_terms()
{
text_terms = new TextTermEntry[text_terms_size];
}
Code:
TextEntity* unsorted_collection; //Array to hold objects representing the text files (unsorted) TextEntity* text_collection; //Array to hold objects representing the text files (sorted)
Code:
void swap_texts(TextEntity* text_1, TextEntity* text_2)
{
TextEntity* temp = new TextEntity(*text_1);
*text_1 = *text_2;
*text_1->text_terms = *text_2->text_terms;
*text_2 = *temp;
*text_2->text_terms = *temp->text_terms;
}
Because I want to resort the now sorted unsorted_collection array for my parallel distribution of the objects I do the following:
Code:
text_collection = new TextEntity[text_collection_size];
int local_texts_size = text_collection_size / size;
int index = text_collection_size - 1;
//Copy the sorted data accesing the index as described above
for (int j = 0; j < local_texts_size; j++)
for (int i = 0; i < size; i++)
{
text_collection[local_texts_size * i + j] = unsorted_collection[index];
text_collection[local_texts_size * i + j].text_terms = unsorted_collection[index].text_terms;
index--;
}
Although my code works it doesn't free any memory... and I'm pretty confused with what I've done

If I try to
Code:
delete [] unsorted_collection;
and even if I leave the unsorted_collection array as is and at least try to
run:
Code:
void TextEntity::delete_text_terms()
{
delete [] text_terms;
text_terms = NULL;
}
.
.
.
for (int i = local_collection_size; i < text_collection_size; i++)
text_collection[i].delete_text_terms();
.
.
.
Thanks!
