diff --git a/assignments/03/.gitignore b/assignments/03/.gitignore index cca9c0b..971d284 100644 --- a/assignments/03/.gitignore +++ b/assignments/03/.gitignore @@ -1,4 +1,5 @@ lpa compile_commands.json .cache -report.pdf \ No newline at end of file +report.pdf +*.tar.gz \ No newline at end of file diff --git a/assignments/03/Makefile b/assignments/03/Makefile index acaba41..4b36aa1 100644 --- a/assignments/03/Makefile +++ b/assignments/03/Makefile @@ -1,13 +1,14 @@ .PHONY: run clean -CFLAGS += -DFMT_HEADER_ONLY -LDFLAGS += $(shell pkg-config --libs fmt) +CFLAGS += -O3 -g +# CFLAGS += -DFMT_HEADER_ONLY +# LDFLAGS += $(shell pkg-config --libs fmt) clean: rm -f lpa lpa: lpa.cpp - mpic++ $(CFLAGS) $(LDFLAGS) -o $@ -g $< + mpic++ $(CFLAGS) $(LDFLAGS) -o $@ $< run: watchexec -c clear 'make lpa && mpirun -n 4 ./lpa dataset/both_1000.txt' diff --git a/assignments/03/lpa.cpp b/assignments/03/lpa.cpp index 2dcd295..36293fb 100644 --- a/assignments/03/lpa.cpp +++ b/assignments/03/lpa.cpp @@ -27,6 +27,15 @@ typedef struct { } pair; void init_pair_type(MPI_Datatype *out); +struct pair_vector { + pair *ptr; + int cap; + int len; +}; +void pair_vector_init(struct pair_vector *); +void pair_vector_clear(struct pair_vector *); +void pair_vector_push(struct pair_vector *v, int fst, int snd); + int main(int argc, char **argv) { MPI::Init(argc, argv); int rank = MPI::COMM_WORLD.Get_rank(), p = MPI::COMM_WORLD.Get_size(); @@ -75,7 +84,9 @@ int main(int argc, char **argv) { int counts[p], displs[p]; if (rank == 0) { line = NULL; - pair all_edges[total_num_edges]; + // pair all_edges[total_num_edges]; + struct pair_vector all_edges; + pair_vector_init(&all_edges); // For the current process, what's the last node we're expecting to see? int current_process = 0; @@ -92,11 +103,11 @@ int main(int argc, char **argv) { if (current_process == 0) { num_my_edges = edge_counter; my_edges = (pair *)calloc(num_my_edges, sizeof(pair)); - memcpy(my_edges, all_edges, edge_counter * sizeof(pair)); + memcpy(my_edges, all_edges.ptr, edge_counter * sizeof(pair)); } else { MPI_Send(&edge_counter, 1, MPI_INT, current_process, TAG_SEND_NUM_EDGES, MPI::COMM_WORLD); - MPI_Send(all_edges, edge_counter, IntPairType, current_process, + MPI_Send(all_edges.ptr, edge_counter, IntPairType, current_process, TAG_SEND_EDGES, MPI::COMM_WORLD); } @@ -104,11 +115,10 @@ int main(int argc, char **argv) { current_process += 1; current_node_range = node_range(current_process); edge_counter = 0; + pair_vector_clear(&all_edges); } - all_edges[edge_counter].fst = fst; - all_edges[edge_counter].snd = snd; - + pair_vector_push(&all_edges, fst, snd); edge_counter += 1; } @@ -116,18 +126,8 @@ int main(int argc, char **argv) { // the loop above MPI_Send(&edge_counter, 1, MPI_INT, current_process, TAG_SEND_NUM_EDGES, MPI::COMM_WORLD); - MPI_Send(all_edges, edge_counter, IntPairType, current_process, + MPI_Send(all_edges.ptr, edge_counter, IntPairType, current_process, TAG_SEND_EDGES, MPI::COMM_WORLD); - - // int step = num_edges / p; - // for (int i = 0; i < p; ++i) { - // int start = i * step; - // int end = i == p - 1 ? num_edges : start + step; - // int count = end - start; - - // counts[i] = count; - // displs[i] = start; - // } } else { MPI_Recv(&num_my_edges, 1, MPI_INT, 0, TAG_SEND_NUM_EDGES, MPI::COMM_WORLD, NULL); @@ -316,8 +316,10 @@ int main(int argc, char **argv) { MPI::COMM_WORLD.Barrier(); double end_time = MPI::Wtime(); - printf("2-5 Time: %0.04fs\n", end_time - step_2_start_time); - printf("5 Time: %0.04fs\n", end_time - step_5_start_time); + if (rank == 0) { + printf("2-5 Time: %0.04fs\n", end_time - step_2_start_time); + printf("5 Time: %0.04fs\n", end_time - step_5_start_time); + } // The results are gathered to a single process, which writes them to the // disk. @@ -369,4 +371,30 @@ void init_pair_type(MPI_Datatype *out) { MPI_Type_create_struct(2, blocklengths, offsets, types, out); MPI_Type_commit(out); +} + +void pair_vector_init(struct pair_vector *v) { + const int INITIAL = 100; + v->ptr = (pair *)malloc(INITIAL * sizeof(pair)); + v->cap = INITIAL; + v->len = 0; +} + +void pair_vector_clear(struct pair_vector *v) { v->len = 0; } + +void pair_vector_push(struct pair_vector *v, int fst, int snd) { + if (v->len == v->cap) { + v->cap *= 2; + pair *new_loc = (pair *)malloc(v->cap * sizeof(pair)); + for (int i = 0; i < v->len; ++i) { + new_loc[i].fst = v->ptr[i].fst; + new_loc[i].snd = v->ptr[i].snd; + } + free(v->ptr); + v->ptr = new_loc; + } + + v->ptr[v->len].fst = fst; + v->ptr[v->len].snd = snd; + v->len++; } \ No newline at end of file diff --git a/assignments/03/zhan4854.tar.gz b/assignments/03/zhan4854.tar.gz deleted file mode 100644 index 297483a..0000000 Binary files a/assignments/03/zhan4854.tar.gz and /dev/null differ