From d0d0f8140a6c50a700f48447df82a83e7680e4d5 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 25 Nov 2023 02:49:11 +0000 Subject: [PATCH] segfaultfix --- assignments/03/.gitignore | 3 +- assignments/03/Makefile | 7 ++-- assignments/03/lpa.cpp | 66 +++++++++++++++++++++++---------- assignments/03/zhan4854.tar.gz | Bin 7478 -> 0 bytes 4 files changed, 53 insertions(+), 23 deletions(-) delete mode 100644 assignments/03/zhan4854.tar.gz 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 297483ab75849bcf45355f937902f023734486c3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7478 zcmV-69m(P!iwFSIS7BuU1MNNAa@$CDPAaKnu$9N;xmOtrnx-^Wt}{F(-bD`zEW;wVJg?tGUo< zvRb{_Xw5%hwNFPzvr!n+gYD{r)w0#88<2{ikyM<=_3)gFk=pV8?dY z%YF7elNXjf_zM0t;NNfIA3p!<&;I+L{(AfA9SrjRns&XiFJ5rF$D`_}vjaEkf4!E||5mLw_kdm8Jt^bY z7q0)!8XG3=kay|}wc4|VXN|>rrMXzI%`epF7Od6++ur(ib#Lv()~_}y7j}|Fl}YN_ zS^eee>a#b_cJu8ow+_|l7aA2;bg>Pyc5*XyAF ztxrgkzP@n%*YjUFUfti{dcM1}v3pP%_U^I|;K%&j+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