Skip to content

Commit 9f3fe09

Browse files
committed
Parallelize buildEngine loop and initializeEngine index building in OMP version (fixes #44, #41)
1 parent 7f53ee7 commit 9f3fe09

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

engine/omp/buildEngine-omp.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,28 @@ node *loadIntoBplusTree(record **records, int num_records, const char *attribute
4343
// Instantiate the B+ tree root
4444
node *root = NULL;
4545

46-
// Iterate through each record and insert into the B+ tree
46+
// Parallelize the iteration and insertion into the B+ tree
47+
// Use critical section to protect tree modifications
48+
#pragma omp parallel for schedule(dynamic)
4749
for (int i = 0; i < num_records; i++) {
4850

49-
// Extract the current record as a key
51+
// Extract the current record as a key (can be done in parallel)
5052
record *currentRecord = records[i];
5153
KEY_T key = extract_key_from_record(currentRecord, attributeName);
5254

5355
// Insert the record into the B+ tree using the key
54-
root = insert(root, key, (ROW_PTR)currentRecord);
56+
// Critical section needed as tree structure is modified
57+
#pragma omp critical
58+
{
59+
root = insert(root, key, (ROW_PTR)currentRecord);
60+
}
5561

5662
// Validate insertion via printing (only in verbose mode)
5763
if (VERBOSE) {
58-
printTree(root);
64+
#pragma omp critical
65+
{
66+
printTree(root);
67+
}
5968
}
6069
}
6170

engine/omp/executeEngine-omp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,13 @@ struct engineS *initializeEngineSerial(
815815
engine->datafile = strdup(datafile);
816816
engine->all_records = getAllRecordsFromFile(datafile, &engine->num_records); // Directly update record count
817817

818-
// Copy indexed attribute names and types into engine struct (defaults)
818+
// Build B+ tree indexes in parallel - each index is independent
819+
#pragma omp parallel for schedule(dynamic)
819820
for (int i = 0; i < num_indexes; i++) {
820821
// Build B+ tree index for each indexed attribute
821822
bool success = makeIndexSerial(engine, indexed_attributes[i], attribute_types[i]);
822823
if (!success) {
824+
#pragma omp critical
823825
fprintf(stderr, "Failed to create index for attribute: %s\n", indexed_attributes[i]);
824826
}
825827
}

0 commit comments

Comments
 (0)