-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystruct.cpp
97 lines (80 loc) · 2.64 KB
/
mystruct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @file mystruct.cpp
* @author Patrick Flick <[email protected]>
*
* Copyright (c) 2016 Georgia Institute of Technology. All Rights Reserved.
*/
#include "mystruct.h"
#include <iostream>
#include <cstdlib>
/**
* Returns the MPI_Datatype for `MyStruct`.
*
* TODO: You have to implement this function here:
*/
MPI_Datatype mystruct_get_mpi_type() {
// use MPI commands to create a custom data type for MyStruct
// TODO: create the MPI datatype for MyStruct
int blens[3] = {1, 1, 4};
MPI_Datatype types[3] = {MPI_UNSIGNED,MPI_DOUBLE,MPI_CHAR};
MyStruct a;
MPI_Aint base, adr_key, adr_d, adr_e;
MPI_Get_address(&a, &base);
MPI_Get_address(&a.key, &adr_key);
MPI_Get_address(&a.d, &adr_d);
MPI_Get_address(&a.e[0], &adr_e);
MPI_Aint disps[3] = {adr_key - base, adr_d - base, adr_e - base};
MPI_Aint extend = sizeof(a);
MPI_Datatype mpi_tmp_t, type;
MPI_Type_create_struct(3, blens, disps, types, &mpi_tmp_t);
MPI_Type_create_resized(mpi_tmp_t, 0, extend, &type);
MPI_Type_commit(&type);
return type;
}
// struct A { char c;
// double d;
// char e[3]; };
// A a;
// MPI_Aint base, adr_c, adr_d, adr_e;
// MPI_Get_address(&a, &base);
// MPI_Get_address(&a.c, &adr_c);
// MPI_Get_address(&a.d, &adr_d);
// MPI_Get_address(&a.e[0], &adr_e);
// MPI_Aint disps[3] = {adr_c - base, adr_d - base, adr_e - base};
// MPI_Aint extend = sizeof(a);
// MPI_Type_create_struct(3, blens, types, disps, &mpi_tmp_t);
/*********************************************************************
* Don't change anything beyond this point *
*********************************************************************/
/**
* Returns a random instance of `MyStruct`.
*/
MyStruct mystruct_rand() {
MyStruct result;
result.key = rand();
result.d = rand()*1.0/RAND_MAX;
result.e[0] = 'a' + rand() % 26;
result.e[1] = 'a' + rand() % 26;
result.e[2] = 'a' + rand() % 26;
result.e[3] = 0;
return result;
}
/**
* Returns the value of the key of `MyStruct`.
*/
unsigned int mystruct_key_access(const MyStruct& s) {
return s.key;
}
// output format for MyStruct
std::ostream& operator <<(std::ostream& stream, const MyStruct& s) {
stream << "{key=" << s.key << ",d=" << s.d << ",e=\"" << s.e << "\"}";
return stream;
}
// operator used for comparison based sorting
bool operator<(const MyStruct& lhs, const MyStruct& rhs) {
return lhs.key < rhs.key;
}
// operator for comparing if two MyStruct's are equal
bool operator==(const MyStruct& lhs, const MyStruct& rhs) {
return (lhs.key == rhs.key) && (lhs.d == rhs.d) && (std::string(lhs.e) == std::string(rhs.e));
}