ALL 0.9.3
A Loadbalacing Library
Loading...
Searching...
No Matches
TestCompare.c
Go to the documentation of this file.
1/*
2 Copyright 2020-2020 Stephan Schulz, Forschungszentrum Juelich GmbH, Germany
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 3. Neither the name of the copyright holder nor the names of its contributors
15 may be used to endorse or promote products derived from this software without
16 specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <math.h>
35
36#define STB_DEFINE
37#define STB_DEBUG
38#include "stb_arr.h"
39
40// Returns 0 on Match and 2 on Bad Match.
41// Other errors return different codes.
42
43#define EXIT_NOMATCH 2
44
45// This assumes the following format:
46// First two lines .must. be:
47// Ranks: %d
48// Number of Steps: %d
49// Then lines are ignored except
50// [%d,%d,%d] Result Vertex: %f %f %f
51// With: [Step, Rank, Vertex]
52// The lines need not be ordered, but must all be placed after Number
53// of Steps
54// Output: Data[Step][Rank][Vertex][Dimension]
55double ****LoadTest(char *FileName)
56{
57 FILE* File = fopen(FileName,"r");
58 if(!File)
59 {
60 fprintf(stderr,"Could not open file '%s'\n", FileName);
61 exit(EXIT_FAILURE);
62 }
63 const int LineLength = 8192;
64 char Line[LineLength];
65 int Dimension = 3;
66 if(fgets(Line, LineLength, File) == NULL)
67 {
68 fprintf(stderr,"Could not read file '%s'\n", FileName);
69 exit(EXIT_FAILURE);
70 }
71 int Ranks;
72 int ReadCount = sscanf(Line, "Ranks: %d", &Ranks);
73 if(ReadCount<1)
74 {
75 fprintf(stderr,"Could not parse ranks in '%s'\n", FileName);
76 exit(EXIT_FAILURE);
77 }
78 if(fgets(Line, LineLength, File) == NULL)
79 {
80 fprintf(stderr,"Could not read file '%s'\n", FileName);
81 exit(EXIT_FAILURE);
82 }
83 int Steps;
84 ReadCount = sscanf(Line, "Number of Steps: %d", &Steps);
85 if(ReadCount<1)
86 {
87 fprintf(stderr,"Could not parse number of steps in '%s'\n", FileName);
88 exit(EXIT_FAILURE);
89 }
90 double ****Data = NULL;
91 stb_arr_setlen(Data, Steps);
92 memset(Data, 0, sizeof(Data)*Steps);
93 int CurrentStep = 0;
94 while(fgets(Line, LineLength, File) != NULL)
95 {
96 double InRankData[3];
97 int CurrentRank;
98 int CurrentVertex;
99 ReadCount = sscanf(Line, "[%d,%d,%d] Result Vertex: %lf %lf %lf",
100 &CurrentStep,
101 &CurrentRank,
102 &CurrentVertex,
103 &InRankData[0], &InRankData[1], &InRankData[2]);
104 if(ReadCount!=6) continue;
105 CurrentStep--; //The input is 1 indexed, the code 0 indexed.
106 double ***StepData = Data[CurrentStep];
107 if(!stb_arr_valid(StepData, CurrentRank))
108 {
109 // increase array and initialise values
110 int CurrentLength = stb_arr_len(StepData);
111 stb_arr_setlen(StepData, CurrentRank+1);
112 memset(&StepData[CurrentLength], 0, sizeof(StepData)*(CurrentRank+1-CurrentLength));
113 Data[CurrentStep] = StepData;
114 }
115 double **RankData = StepData[CurrentRank];
116 if(!stb_arr_valid(RankData, CurrentVertex))
117 {
118 int CurrentLength = stb_arr_len(RankData);
119 stb_arr_setlen(RankData, CurrentVertex+1);
120 memset(&RankData[CurrentVertex], 0, sizeof(RankData)*(CurrentVertex+1-CurrentLength));
121 StepData[CurrentRank] = RankData;
122 }
123 double *VertexData = RankData[CurrentVertex];
124 stb_arr_setlen(VertexData, Dimension);
125 RankData[CurrentVertex] = VertexData;
126 VertexData[0] = InRankData[0];
127 VertexData[1] = InRankData[1];
128 VertexData[2] = InRankData[2];
129 //printf("IN: %s", Line);
130 //printf("ME: [%4d,%03d,%02d] Result Vertex: %10.6f %10.6f %10.6f\n",
131 // CurrentStep+1,
132 // CurrentRank,
133 // CurrentVertex,
134 // InRankData[0], InRankData[1], InRankData[2]);
135 }
136 fclose(File);
137 return Data;
138}
139
140// return 0 on difference and >1 if same
141int CompareTests(double ****Test1, double ****Test2)
142{
143 double MaximumDifference = 0;
144 int CurrentStep, CurrentRank, CurrentVertex, i;
145 if(stb_arr_len(Test1) != stb_arr_len(Test2)){
146 printf("Different number of steps: %d %d\n", stb_arr_len(Test1), stb_arr_len(Test2));
147 return 0;
148 }
149 //printf("Steps: %d %d\n", stb_arr_len(Test1), stb_arr_len(Test2));
150 for(CurrentStep=0; CurrentStep<stb_arr_len(Test1); CurrentStep++)
151 {
152 if(stb_arr_len(Test1[CurrentStep]) != stb_arr_len(Test2[CurrentStep]))
153 {
154 printf("Different number of ranks in Step %d: %d %d\n",
155 CurrentStep,
156 stb_arr_len(Test1[CurrentStep]),
157 stb_arr_len(Test2[CurrentStep]));
158 return 0;
159 }
160 //printf(" Step: %3d Ranks: %d %d\n", CurrentStep, stb_arr_len(Test1[CurrentStep]), stb_arr_len(Test2[CurrentStep]));
161 for(CurrentRank=0; CurrentRank<stb_arr_len(Test1[CurrentStep]); CurrentRank++)
162 {
163 if(stb_arr_len(Test1[CurrentStep][CurrentRank]) != stb_arr_len(Test2[CurrentStep][CurrentRank]))
164 {
165 printf("Different number of vertices in Step %d and Rank %d: %d %d\n",
166 CurrentStep,
167 CurrentRank,
168 stb_arr_len(Test1[CurrentStep][CurrentRank]),
169 stb_arr_len(Test2[CurrentStep][CurrentRank]));
170 return 0;
171 }
172 for(CurrentVertex=0; CurrentVertex<stb_arr_len(Test1[CurrentStep][CurrentRank]); CurrentVertex++)
173 {
174 assert(stb_arr_valid(Test2[CurrentStep][CurrentRank], CurrentVertex));
175 for(i=0; i<stb_arr_len(Test1[CurrentStep][CurrentRank][CurrentVertex]); i++)
176 {
177 double Value1 = Test1[CurrentStep][CurrentRank][CurrentVertex][i];
178 double Value2 = Test2[CurrentStep][CurrentRank][CurrentVertex][i];
179 double AbsVal = fabs(Value1-Value2);
180 MaximumDifference = MaximumDifference>AbsVal?MaximumDifference:AbsVal;
181 if(AbsVal>0.001) printf("Found deviation of %g at Step: %u Rank: %u Vertex: %u, between %g and %g\n",
182 AbsVal,
183 CurrentStep,
184 CurrentRank,
185 CurrentVertex,
186 Value1, Value2);
187 }
188 }
189 }
190 }
191 printf("MaximumDifference: %g\n", MaximumDifference);
192 return MaximumDifference<0.001;
193}
194
195int main(int argc, char **argv)
196{
197 if(argc<3)
198 {
199 printf("Usage: %s KNOWN_GOOD TEST_OUTPUT\n", argv[0]);
200 return EXIT_FAILURE;
201 }
202 char *GoodFileName = argv[1];
203 char *TestFileName = argv[2];
204
205 // TestData2[Step][Rank][Vertex][Dimension]
206 double ****GoodData = LoadTest(GoodFileName);
207 double ****TestData = LoadTest(TestFileName);
208
209 int Matches = CompareTests(GoodData, TestData);
210
211 if(Matches)
212 return EXIT_SUCCESS;
213 else
214 return EXIT_NOMATCH;
215}
int main(int argc, char **argv)
int CompareTests(double ****Test1, double ****Test2)
#define EXIT_NOMATCH
Definition TestCompare.c:43
double **** LoadTest(char *FileName)
Definition TestCompare.c:55