@@ -69,43 +69,110 @@ node *loadIntoBplusTreeMPI(record **records, int num_records, const char *attrib
6969 * Array of all record structs in the file
7070*/
7171record * * getAllRecordsFromFileMPI (const char * filepath , int * num_records ) {
72- // Attempt to open the file from the provided path
73- FILE * file = fopen (filepath , "r" );
74- if (file == NULL ) {
75- fprintf (stderr , "Error opening file: %s\n" , filepath );
76- return NULL ;
72+ int rank ;
73+ MPI_Comm_rank (MPI_COMM_WORLD , & rank );
74+
75+ char * file_content = NULL ;
76+ long file_size = 0 ;
77+
78+ // Rank 0 reads the file from disk
79+ if (rank == 0 ) {
80+ FILE * file = fopen (filepath , "r" );
81+ if (file == NULL ) {
82+ fprintf (stderr , "Error opening file: %s\n" , filepath );
83+ file_size = -1 ; // Signal error
84+ } else {
85+ fseek (file , 0 , SEEK_END );
86+ file_size = ftell (file );
87+ fseek (file , 0 , SEEK_SET );
88+
89+ file_content = (char * )malloc (file_size + 1 );
90+ if (file_content ) {
91+ size_t read_size = fread (file_content , 1 , file_size , file );
92+ if (read_size != (size_t )file_size ) {
93+ fprintf (stderr , "Error reading file content\n" );
94+ free (file_content );
95+ file_content = NULL ;
96+ file_size = -1 ;
97+ } else {
98+ file_content [file_size ] = '\0' ;
99+ }
100+ } else {
101+ file_size = -1 ; // Memory error
102+ }
103+ fclose (file );
104+ }
105+ }
106+
107+ // Broadcast file size to all ranks
108+ MPI_Bcast (& file_size , 1 , MPI_LONG , 0 , MPI_COMM_WORLD );
109+
110+ if (file_size < 0 ) {
111+ return NULL ; // Propagate error
77112 }
78113
79- // Initialize the records and count
114+ // Allocate memory on other ranks
115+ if (rank != 0 ) {
116+ file_content = (char * )malloc (file_size + 1 );
117+ if (!file_content ) {
118+ fprintf (stderr , "Rank %d failed to allocate memory for file content\n" , rank );
119+ // In a real app, we should signal abort here
120+ return NULL ;
121+ }
122+ }
123+
124+ // Broadcast file content to all ranks
125+ // Note: MPI_Bcast count is int. If file_size > INT_MAX, this needs chunking.
126+ // For this project, we assume file_size fits in int.
127+ MPI_Bcast (file_content , (int )file_size + 1 , MPI_CHAR , 0 , MPI_COMM_WORLD );
128+
129+ // Parse records from memory buffer
80130 record * * records = NULL ;
81- char line [1024 ];
82131 int count = 0 ;
132+
133+ char * cursor = file_content ;
134+
135+ // Skip header line
136+ char * eol = strchr (cursor , '\n' );
137+ if (eol ) {
138+ cursor = eol + 1 ;
139+ }
83140
84- // Read each line and populate the records array
85- bool first_line = true;
86- while (fgets (line , sizeof (line ), file )) {
87- if (first_line ) {
88- first_line = false;
89- continue ; // Skip header
141+ while (* cursor ) {
142+ // Find end of line
143+ eol = strchr (cursor , '\n' );
144+ if (!eol ) {
145+ // Handle last line without newline
146+ if (* cursor ) eol = cursor + strlen (cursor );
147+ else break ;
90148 }
91- record * new_record = getRecordFromLineMPI (line );
92- records = realloc (records , (count + 1 ) * sizeof (record * ));
93- if (records == NULL ) {
94- fprintf (stderr , "Memory allocation failed\n" );
95- fclose (file );
96- return NULL ;
149+
150+ if (eol > cursor ) {
151+ // Temporarily null-terminate the line to use existing parser
152+ char save = * eol ;
153+ * eol = '\0' ;
154+
155+ record * new_record = getRecordFromLineMPI (cursor );
156+ if (new_record ) {
157+ records = realloc (records , (count + 1 ) * sizeof (record * ));
158+ records [count ] = new_record ;
159+ count ++ ;
160+ }
161+
162+ * eol = save ; // Restore (good practice)
97163 }
98- records [count ] = new_record ;
99- count ++ ;
164+
165+ if (* eol == '\0' ) break ;
166+ cursor = eol + 1 ;
100167 }
101168
102- // Close the file, set the output count, and return the records array
103- fclose ( file );
104- if (VERBOSE ) {
169+ free ( file_content );
170+
171+ if (VERBOSE && rank == 0 ) {
105172 printf ("Loaded %d records from file: %s\n" , count , filepath );
106173 }
107174 * num_records = count ;
108- return records ; // Return the array of all records
175+ return records ;
109176}
110177
111178/* Helper to parse a CSV field, handling quotes and commas */
0 commit comments