segfaultfix

This commit is contained in:
Michael Zhang 2023-11-25 02:49:11 +00:00
parent fac66e8e70
commit d0d0f8140a
4 changed files with 53 additions and 23 deletions

View file

@ -2,3 +2,4 @@ lpa
compile_commands.json compile_commands.json
.cache .cache
report.pdf report.pdf
*.tar.gz

View file

@ -1,13 +1,14 @@
.PHONY: run clean .PHONY: run clean
CFLAGS += -DFMT_HEADER_ONLY CFLAGS += -O3 -g
LDFLAGS += $(shell pkg-config --libs fmt) # CFLAGS += -DFMT_HEADER_ONLY
# LDFLAGS += $(shell pkg-config --libs fmt)
clean: clean:
rm -f lpa rm -f lpa
lpa: lpa.cpp lpa: lpa.cpp
mpic++ $(CFLAGS) $(LDFLAGS) -o $@ -g $< mpic++ $(CFLAGS) $(LDFLAGS) -o $@ $<
run: run:
watchexec -c clear 'make lpa && mpirun -n 4 ./lpa dataset/both_1000.txt' watchexec -c clear 'make lpa && mpirun -n 4 ./lpa dataset/both_1000.txt'

View file

@ -27,6 +27,15 @@ typedef struct {
} pair; } pair;
void init_pair_type(MPI_Datatype *out); 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) { int main(int argc, char **argv) {
MPI::Init(argc, argv); MPI::Init(argc, argv);
int rank = MPI::COMM_WORLD.Get_rank(), p = MPI::COMM_WORLD.Get_size(); 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]; int counts[p], displs[p];
if (rank == 0) { if (rank == 0) {
line = NULL; 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? // For the current process, what's the last node we're expecting to see?
int current_process = 0; int current_process = 0;
@ -92,11 +103,11 @@ int main(int argc, char **argv) {
if (current_process == 0) { if (current_process == 0) {
num_my_edges = edge_counter; num_my_edges = edge_counter;
my_edges = (pair *)calloc(num_my_edges, sizeof(pair)); 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 { } else {
MPI_Send(&edge_counter, 1, MPI_INT, current_process, MPI_Send(&edge_counter, 1, MPI_INT, current_process,
TAG_SEND_NUM_EDGES, MPI::COMM_WORLD); 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); TAG_SEND_EDGES, MPI::COMM_WORLD);
} }
@ -104,11 +115,10 @@ int main(int argc, char **argv) {
current_process += 1; current_process += 1;
current_node_range = node_range(current_process); current_node_range = node_range(current_process);
edge_counter = 0; edge_counter = 0;
pair_vector_clear(&all_edges);
} }
all_edges[edge_counter].fst = fst; pair_vector_push(&all_edges, fst, snd);
all_edges[edge_counter].snd = snd;
edge_counter += 1; edge_counter += 1;
} }
@ -116,18 +126,8 @@ int main(int argc, char **argv) {
// the loop above // the loop above
MPI_Send(&edge_counter, 1, MPI_INT, current_process, TAG_SEND_NUM_EDGES, MPI_Send(&edge_counter, 1, MPI_INT, current_process, TAG_SEND_NUM_EDGES,
MPI::COMM_WORLD); 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); 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 { } else {
MPI_Recv(&num_my_edges, 1, MPI_INT, 0, TAG_SEND_NUM_EDGES, MPI::COMM_WORLD, MPI_Recv(&num_my_edges, 1, MPI_INT, 0, TAG_SEND_NUM_EDGES, MPI::COMM_WORLD,
NULL); NULL);
@ -316,8 +316,10 @@ int main(int argc, char **argv) {
MPI::COMM_WORLD.Barrier(); MPI::COMM_WORLD.Barrier();
double end_time = MPI::Wtime(); double end_time = MPI::Wtime();
if (rank == 0) {
printf("2-5 Time: %0.04fs\n", end_time - step_2_start_time); 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); 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 // The results are gathered to a single process, which writes them to the
// disk. // disk.
@ -370,3 +372,29 @@ void init_pair_type(MPI_Datatype *out) {
MPI_Type_create_struct(2, blocklengths, offsets, types, out); MPI_Type_create_struct(2, blocklengths, offsets, types, out);
MPI_Type_commit(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++;
}

Binary file not shown.