I am working with CUDA and I am trying to stop my kernels work (i.e. terminate all running threads) after a certain if block is being hit. How can I do that? I am really stuck in here.
The CUDA execution model doesn't allow for inter-block communication by design. That can potentially make this sort of kernel abort on condition operation difficult to achieve reliably without resorting to the assert or trap type approaches which can potentially result in context destruction and loss of data which isn't what you probably want.
If your kernel design involves a small number of blocks with "resident" threads, then the only approach is some sort of atomic spinlock, which is hard to get to work reliably, and which will greatly degrade memory controller performance and achievable bandwidth.
If, on the other hand, your kernel design has rather large grids with a lot of blocks, and your main goal is to stop blocks which are not yet scheduled from running, then you could try something like this:
#include <iostream>
#include <vector>
__device__ unsigned int found_idx;
__global__ void setkernel(unsigned int *indata)
{
indata[115949] = 0xdeadbeef;
indata[119086] = 0xdeadbeef;
indata[60534] = 0xdeadbeef;
indata[37072] = 0xdeadbeef;
indata[163107] = 0xdeadbeef;
}
__global__ void searchkernel(unsigned int *indata, unsigned int *outdata)
{
if (found_idx > 0) {
return;
} else if (threadIdx.x == 0) {
outdata[blockIdx.x] = blockIdx.x;
};
unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (indata[tid] == 0xdeadbeef) {
unsigned int oldval = atomicCAS(&found_idx, 0, 1+tid);
}
}
int main()
{
const unsigned int N = 1 << 19;
unsigned int* in_data;
cudaMalloc((void **)&in_data, sizeof(unsigned int) * size_t(N));
cudaMemset(in_data, 0, sizeof(unsigned int) * size_t(N));
setkernel<<<1,1>>>(in_data);
cudaDeviceSynchronize();
unsigned int block_size = 1024;
unsigned int grid_size = N / block_size;
unsigned int* out_data;
cudaMalloc((void **)&out_data, sizeof(unsigned int) * size_t(grid_size));
cudaMemset(out_data, 0xf0, sizeof(unsigned int) * size_t(grid_size));
const unsigned int zero = 0;
cudaMemcpyToSymbol(found_idx, &zero, sizeof(unsigned int));
searchkernel<<<grid_size, block_size>>>(in_data, out_data);
std::vector<unsigned int> output(grid_size);
cudaMemcpy(&output[0], out_data, sizeof(unsigned int) * size_t(grid_size), cudaMemcpyDeviceToHost);
cudaDeviceReset();
std::cout << "The following blocks did not run" << std::endl;
for(int i=0, j=0; i<grid_size; i++) {
if (output[i] == 0xf0f0f0f0) {
std::cout << " " << i;
if (j++ == 20) {
std::cout << std::endl;
j = 0;
}
}
}
std::cout << std::endl;
return 0;
}
Here I have a simple kernel which is searching for a magic word in a large array. To get the early exit behaviour, I use a single global word, which is set atomically by those threads which "win" or trigger the termination condition. Every new block checks the state of this global word, and if it is set, they return without doing any work.
If I compile and run this on a moderate sized Kepler device:
$ nvcc -arch=sm_30 -o blocking blocking.cu
$ ./blocking
The following blocks did not run
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 98 99 100 101 102 103 104
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
504 505 506 507 508 509 510 511
you can see that a large number of blocks in the grid saw the change in the global word and early terminated without running the search code. This might be the best you can do without a severely invasive spinlock approach which will greatly harm performance.
I assume you want to stop a running kernel (not a single thread).
The simplest approach (and the one that I suggest) is to set up a global memory flag which is been tested by the kernel.
You can set the flag using cudaMemcpy() (or without if using unified memory).
Like the following:
if (gm_flag) {
__threadfence(); // ensure store issued before trap
asm("trap;"); // kill kernel with error
}
ams("trap;") will stop all running thread
Note that since cuda 2.0 you can use assert() to terminate a kernel!
A different approach could be the following (I haven't tried the code!)
__device__ bool go(int val){
return true;
}
__global__ void stopme(bool* flag, int* val, int size){
int idx= blockIdx.x *blockDim.x + threadIdx.x;
if(idx < size){
bool canContinue = true;
while(canContinue && (flag[0])){
printf("HELLO from %i\n",idx);
if(!(*flag)){
return;
}
else{
//do some computation
val[idx]++;
val[idx]%=100;
}
canContinue = go(val[idx]);
}
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int main(void)
{
int size = 128;
int* h_val = (int*)malloc(sizeof(int)*size);
bool * h_flag = new bool;
*h_flag=true;
bool* d_flag;
cudaMalloc(&d_flag,sizeof(bool));
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
int* d_val;
cudaMalloc(&d_val,sizeof(int)*size );
for(int i=0;i<size;i++){
h_val[i] = i;
}
cudaMemcpy(d_val,h_val,size,cudaMemcpyHostToDevice);
int BSIZE=32;
int nblocks =size/BSIZE;
printf("%i,%i",nblocks,BSIZE);
stopme<<<nblocks,BSIZE>>>(d_flag,d_val,size);
//--------------sleep for a while --------------------------
*h_flag=false;
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
gpuErrchk( cudaPeekAtLastError() );
printf("END\n");
}
where the kernel stopMe keeps running until someone from the host side sets up the flag to false. Note that your kernel could be much more complicated than this and the effort to synchronize all threads in order to execute the return could be much more than this (and can affect performance). Hope this helped.
More info here
Related
I've written the code for eliminating the largest 2 elements of an array, but this code gives junk value for testcase > 1. Why?
Input:
no of TestCase
size of array
elements of array
Sorting function:
int sort_asc(int arr[], int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main() {
//code
int test;
cin>>test;
while(test--){
//taking size and array as inputs
int size;
cin>>size;
int a[size];
cin>>a[size];
for(int i=0;i<size;i++){
cin>>a[i];
}
//sorting the array
sort_asc(a,size);
//printing the output discarding last 2 elements of the array
for(int i=0;i<size-2;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
return 0;
}
Expected:
12 23 28 43 44 59 60 68 70 85 88 92 124 125 136 168 171 173 179 199 212
230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414
422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751
764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 927 930 957
My output:
12 23 28 43 44 59 60 68 70 81 85 88 92 124 125 136 168 171 173 179 199 212 230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414 422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751 764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 930 957
A VLA (variable length array) is invalid C++ code. It is tolerated by some compilers, but it is still invalid.
But that is not your main problem. You produced an out of bound error. An array index starts with 0. The last element is at position size-1. So your statement
cin>>a[size];
will write past the end of your array. Producing undefined behavior.
I am not sure, why you put the statement at all, but after that, anything undefined can and most probably will happen.
I am working on a programme which takes an initial vector as an inpit.This vector is vector of size 20.The programme then generates 10 random vectors from this vector.For this purpose I choose 2 ransom indices in the initial vector and swap them with each other to generate a new vector.This is done to generate all the 10 new vectors.
The 10 new vectors generated should be stored in the following 2 dimensional vector
vector<vector<int>> allparents
I have been able to generate 2 random indice numbers using the srand() function and then swap the elements at these indices for the initial vector.However I am unable to generate 10 of such random parents and then store them in the allparents 2D vector.My code is as follows :
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
int main () {
srand(time(NULL));
int i1=(rand()%19+1);
int i2=(rand()%19+1);
cout<<i1<<endl;
cout<<i2<<endl;
vector<int> initial={71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560};
vector<vector<int>> allparents;
for(int r=0;r<10;r++){
for(int c=0;c<20;c++){
swap(initial[i1],initial[i2]);
allparents[r][c]=initial[c];
cout<<allparents[r][c]<" "<<endl;
}
}
return 0;
}
As I am new to vectors,I would request your help in this programme.Thanks.
First of all, I would not say that you're "generating random vectors", you're just shuffling a predefined one.
Second, I suggest to create small working functions and assemble your program with those:
vector<int> shuffle(vector<int> v) {
// Use the implementation you want here, I will use a std algorithm
static auto rng = std::default_random_engine {};
std::shuffle(std::begin(v), std::end(v), rng);
return v; // this is a copy of the vector
}
int main() {
vector<int> initial= {
71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560
};
// Generate 10 shuffled vectors
vector<vector<int>> shuffledVectors;
for (int i = 0; i < 10; i++) {
vector<int> shuffled = shuffle(initial);
shuffledVectors.push_back(shuffled);
}
// Print them
for (vector<int>& v : shuffledVectors) {
for (int& i : v)
cout << i << " ";
cout << endl;
}
return 0;
}
Output:
164 41 110 305 278 28 58 127 229 189 475 395 560 428 71 443 253 371 490 92
490 71 305 58 428 127 28 110 92 443 189 229 278 475 371 395 560 41 253 164
395 278 560 490 28 164 71 229 58 41 428 305 127 253 475 371 92 189 110 443
164 475 92 253 229 189 127 560 71 58 41 443 428 395 371 490 110 278 28 305
443 253 428 110 278 71 475 127 58 41 371 229 305 189 395 164 28 490 92 560
560 28 58 71 229 41 490 475 189 443 253 395 305 164 371 278 428 92 110 127
395 443 371 58 253 305 92 127 475 110 428 229 189 41 164 278 71 560 28 490
278 189 71 127 443 110 28 428 305 560 371 58 229 253 395 164 41 490 475 92
28 395 92 443 560 278 371 71 58 305 475 253 428 490 229 189 164 110 41 127
443 71 428 229 127 278 490 58 475 253 164 110 92 189 395 560 305 41 28 371
I have a .txt file with tab separated data(several rows):
42 2 62
2 2 42
2 2 62
I tried to use istringstream to read the data from the file, and then use each column's data for each respective row as locations for adding 1's into a 2-D array.
This was to loop until the 2-D array was filled with 1's for each position designated by the loop(rows) and the file data(columns).
Note: the second column is not used, but is put in since this is how the .txt file appears.
I print out each row one by one, and expect there to be three 1's in each row, for all of the rows.
However when I print my output for the 2-D array(code block near bottom), I get unexpected results. For some rows, there will be only two 1's within the row, and for some there will be four 1's.
I am not a very good coder, so any advice would be appreciated, the code is posted below.
#include "stdafx.h"
using namespace std;
//prints the diagonal of array
void printarray(int arg[][300], int length) {
for (int n = 0; n<length; ++n)
cout << arg[n][n] << ' ';
cout << '\n';
}
//makes 2d array values all 0
void zeros(int a[][300], int size)
{
cout << "Setting all elements of array to zero...." << endl;
for (int k = 0; k < (size*size); k++)
{
for (int i = 0; i<size; i++)
{
for (int j = 0; j<size; j++)
{
a[i][j] = 0;
}
}
}
}
int main()
{
//2D array of 300by300
int adMatrix [300][300];
zeros(adMatrix, 300);
int counter = 0;
string line;
int firstVar,secondVar,thirdVar; // variables to hold the bond neighbors while looping
// Read sample file
ifstream infile("myTestFile.txt");
while (counter < 300 ) {
getline(infile, line);
istringstream stream(line);
stream >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream.str("");
stream.clear();
getline(infile, line);
istringstream stream2(line);
stream2 >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream2.str("");
stream2.clear();
getline(infile, line);
istringstream stream3(line);
stream3 >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream3.str("");
stream3.clear();
counter++;
}
// iterates through entire matrix, prints values, also tallys sum of each row, should equal 3 for each row.
int var = 0;
for (int i = 0; i < 300; ++i)
{
int sum = 0;
for (int i = 0; i < 300; ++i)
{
cout << adMatrix[var][i];
sum = adMatrix[var][i] + sum;
}
var++;
cout << endl << "sum is : " << sum << endl;
}
return 0;
}
Here is the data for .txt file I used:
43 1 61
2 1 43
2 1 61
1 2 3
1 2 11
3 2 11
2 3 45
4 3 45
2 3 4
3 4 13
5 4 13
3 4 5
4 5 6
6 5 162
4 5 162
15 6 123
5 6 15
5 6 123
59 7 77
8 7 59
8 7 77
7 8 9
7 8 21
9 8 21
8 9 61
10 9 61
8 9 10
9 10 23
11 10 23
9 10 11
2 11 12
10 11 12
2 11 10
13 12 25
11 12 25
11 12 13
12 13 14
4 13 14
4 13 12
15 14 27
13 14 27
13 14 15
14 15 16
6 15 16
6 15 14
29 16 133
15 16 29
15 16 133
75 17 93
18 17 75
18 17 93
17 18 19
17 18 34
19 18 34
18 19 77
20 19 77
18 19 20
19 20 36
21 20 36
19 20 21
8 21 22
20 21 22
8 21 20
23 22 38
21 22 38
21 22 23
22 23 24
10 23 24
10 23 22
25 24 40
23 24 40
23 24 25
24 25 26
12 25 26
12 25 24
27 26 42
25 26 42
25 26 27
26 27 28
14 27 28
14 27 26
27 28 29
29 28 44
27 28 44
16 29 28
28 29 158
16 29 158
91 30 109
31 30 91
31 30 109
30 31 32
30 31 50
32 31 50
31 32 93
33 32 93
31 32 33
32 33 52
34 33 52
32 33 34
18 34 35
33 34 35
18 34 33
36 35 54
34 35 54
34 35 36
35 36 37
20 36 37
20 36 35
38 37 56
36 37 56
36 37 38
37 38 39
22 38 39
22 38 37
40 39 58
38 39 58
38 39 40
39 40 41
24 40 41
24 40 39
40 41 42
42 41 60
40 41 60
26 42 41
41 42 43
26 42 43
42 43 44
1 43 42
1 43 44
28 44 43
28 44 45
43 44 45
3 45 44
44 45 157
3 45 157
107 46 122
47 46 107
47 46 122
46 47 48
46 47 66
48 47 66
47 48 109
49 48 109
47 48 49
48 49 68
50 49 68
48 49 50
31 50 51
49 50 51
31 50 49
52 51 70
50 51 70
50 51 52
51 52 53
33 52 53
33 52 51
54 53 72
52 53 72
52 53 54
53 54 55
35 54 55
35 54 53
56 55 74
54 55 74
54 55 56
55 56 57
37 56 57
37 56 55
56 57 58
58 57 76
56 57 76
39 58 57
57 58 59
39 58 59
58 59 60
7 59 58
7 59 60
41 60 59
41 60 61
59 60 61
1 61 60
9 61 60
1 61 9
120 62 132
63 62 120
63 62 132
62 63 64
62 63 82
64 63 82
63 64 122
65 64 122
63 64 65
64 65 84
66 65 84
64 65 66
47 66 67
47 66 65
65 66 67
68 67 86
66 67 86
66 67 68
67 68 69
49 68 69
49 68 67
70 69 88
68 69 88
68 69 70
69 70 71
51 70 71
51 70 69
72 71 90
70 71 90
70 71 72
71 72 73
53 72 73
53 72 71
72 73 74
74 73 92
72 73 92
73 74 75
55 74 73
55 74 75
74 75 76
17 75 74
17 75 76
57 76 75
57 76 77
75 76 77
7 77 76
19 77 76
7 77 19
130 78 138
79 78 130
79 78 138
78 79 80
78 79 98
80 79 98
79 80 132
81 80 132
79 80 81
80 81 100
80 81 82
82 81 100
63 82 83
63 82 81
81 82 83
84 83 102
82 83 102
82 83 84
83 84 85
65 84 85
65 84 83
86 85 104
84 85 104
84 85 86
85 86 87
67 86 87
67 86 85
88 87 106
86 87 106
86 87 88
87 88 89
69 88 89
69 88 87
90 89 108
88 89 90
88 89 108
71 90 89
89 90 91
71 90 91
90 91 92
30 91 90
30 91 92
73 92 91
73 92 93
91 92 93
17 93 92
32 93 92
17 93 32
95 94 136
136 94 160
95 94 160
94 95 96
94 95 111
96 95 111
95 96 138
97 96 138
95 96 97
96 97 113
96 97 98
98 97 113
79 98 99
79 98 97
97 98 99
100 99 115
98 99 115
98 99 100
99 100 101
81 100 101
81 100 99
102 101 117
100 101 117
100 101 102
101 102 103
83 102 103
83 102 101
104 103 119
102 103 119
102 103 104
103 104 105
85 104 105
85 104 103
106 105 121
104 105 106
104 105 121
105 106 107
87 106 105
87 106 107
106 107 108
46 107 106
46 107 108
89 108 107
89 108 109
107 108 109
30 109 108
48 109 108
30 109 48
111 110 123
123 110 161
111 110 161
95 111 112
95 111 110
110 111 112
113 112 125
111 112 125
111 112 113
112 113 114
97 113 114
97 113 112
115 114 127
113 114 127
113 114 115
114 115 116
99 115 116
99 115 114
117 116 129
115 116 129
115 116 117
116 117 118
101 117 118
101 117 116
119 118 131
117 118 119
117 118 131
103 119 118
118 119 120
103 119 120
119 120 121
62 120 119
62 120 121
105 121 120
105 121 122
120 121 122
46 122 121
64 122 121
46 122 64
6 123 124
110 123 124
6 123 110
125 124 133
123 124 133
123 124 125
124 125 126
112 125 126
112 125 124
127 126 135
125 126 135
125 126 127
126 127 128
114 127 128
114 127 126
127 128 129
129 128 137
127 128 137
128 129 130
116 129 128
116 129 130
129 130 131
78 130 129
78 130 131
118 131 130
118 131 132
130 131 132
62 132 131
80 132 131
62 132 80
16 133 134
124 133 134
16 133 124
133 134 135
135 134 159
133 134 159
126 135 134
134 135 136
126 135 136
135 136 137
94 136 135
94 136 137
128 137 136
128 137 138
136 137 138
78 138 137
96 138 137
78 138 96
141 139 156
141 139 220
156 139 220
141 140 157
141 140 158
157 140 158
139 141 142
140 141 142
139 141 140
141 142 144
141 142 283
144 142 283
144 143 158
144 143 159
158 143 159
142 144 145
142 144 143
143 144 145
144 145 147
147 145 284
144 145 284
147 146 159
147 146 160
159 146 160
145 147 148
146 147 148
145 147 146
147 148 150
147 148 215
150 148 215
150 149 160
150 149 161
160 149 161
148 150 151
149 150 151
148 150 149
150 151 153
153 151 164
150 151 164
153 152 161
153 152 162
161 152 162
151 153 154
151 153 152
152 153 154
153 154 156
153 154 163
156 154 163
156 155 157
156 155 162
157 155 162
139 156 154
154 156 155
139 156 155
140 157 155
45 157 155
45 157 140
140 158 143
29 158 143
29 158 140
143 159 146
134 159 143
134 159 146
146 160 149
94 160 149
94 160 146
149 161 152
110 161 149
110 161 152
152 162 155
5 162 155
5 162 152
165 163 184
154 163 165
154 163 184
166 164 185
151 164 166
151 164 185
163 165 181
163 165 187
181 165 187
164 166 182
164 166 188
182 166 188
169 167 183
169 167 189
183 167 189
170 168 186
170 168 190
186 168 190
167 169 191
167 169 193
191 169 193
168 170 192
168 170 194
192 170 194
173 171 195
173 171 196
195 171 196
174 172 197
174 172 198
197 172 198
171 173 199
171 173 201
199 173 201
172 174 200
172 174 203
200 174 203
177 175 202
177 175 205
202 175 205
178 176 204
178 176 206
204 176 206
175 177 207
175 177 211
207 177 211
176 178 208
176 178 212
208 178 212
180 179 209
180 179 213
209 179 213
179 180 210
179 180 214
210 180 214
165 181 183
183 181 220
165 181 220
166 182 186
186 182 215
166 182 215
167 183 181
181 183 216
167 183 216
163 184 185
185 184 218
163 184 218
164 185 184
184 185 219
164 185 219
168 186 182
182 186 217
168 186 217
165 187 189
189 187 221
165 187 221
166 188 190
190 188 222
166 188 222
167 189 187
187 189 225
167 189 225
168 190 188
188 190 226
168 190 226
169 191 195
169 191 223
195 191 223
170 192 197
197 192 224
170 192 224
169 193 196
196 193 227
169 193 227
170 194 198
198 194 228
170 194 228
171 195 191
191 195 231
171 195 231
171 196 193
193 196 229
171 196 229
172 197 192
192 197 232
172 197 232
172 198 194
194 198 230
172 198 230
173 199 202
202 199 233
173 199 233
174 200 204
204 200 234
174 200 234
173 201 205
205 201 235
173 201 235
175 202 199
199 202 237
175 202 237
174 203 206
206 203 236
174 203 236
176 204 200
200 204 238
176 204 238
175 205 201
201 205 243
175 205 243
176 206 203
203 206 244
176 206 244
177 207 209
177 207 239
209 207 239
178 208 210
210 208 240
178 208 240
179 209 207
207 209 241
179 209 241
180 210 208
208 210 242
180 210 242
177 211 213
177 211 245
213 211 245
178 212 214
214 212 246
178 212 246
179 213 211
211 213 247
179 213 247
180 214 212
212 214 248
180 214 248
182 215 250
148 215 182
148 215 250
183 216 223
183 216 251
223 216 251
186 217 224
224 217 254
186 217 254
184 218 221
184 218 252
221 218 252
185 219 222
222 219 253
185 219 253
181 220 249
139 220 181
139 220 249
187 221 218
218 221 255
187 221 255
188 222 219
219 222 256
188 222 256
191 223 216
216 223 259
191 223 259
192 224 217
217 224 260
192 224 260
189 225 227
227 225 257
189 225 257
190 226 228
228 226 258
190 226 258
193 227 225
225 227 261
193 227 261
194 228 226
226 228 262
194 228 262
196 229 233
196 229 264
233 229 264
198 230 234
234 230 266
198 230 266
195 231 235
195 231 263
235 231 263
197 232 236
236 232 265
197 232 265
199 233 229
229 233 267
199 233 267
200 234 230
230 234 268
200 234 268
201 235 231
231 235 269
201 235 269
203 236 232
232 236 271
203 236 271
202 237 239
239 237 270
202 237 270
204 238 240
204 238 272
240 238 272
207 239 237
237 239 275
207 239 275
208 240 238
238 240 276
208 240 276
209 241 242
209 241 277
242 241 277
210 242 241
241 242 278
210 242 278
205 243 245
205 243 273
245 243 273
206 244 246
246 244 274
206 244 274
211 245 243
243 245 279
211 245 279
212 246 244
244 246 280
212 246 280
213 247 248
213 247 281
248 247 281
214 248 247
247 248 282
214 248 282
220 249 251
251 249 285
220 249 285
215 250 254
215 250 286
254 250 286
216 251 249
249 251 287
216 251 287
218 252 253
218 252 283
253 252 283
219 253 252
252 253 284
219 253 284
217 254 250
250 254 288
217 254 288
221 255 257
221 255 285
257 255 285
222 256 258
222 256 286
258 256 286
225 257 255
255 257 287
225 257 287
226 258 256
256 258 288
226 258 288
223 259 263
223 259 289
263 259 289
224 260 265
265 260 290
224 260 290
227 261 264
227 261 289
264 261 289
228 262 266
228 262 290
266 262 290
231 263 259
259 263 291
231 263 291
229 264 261
261 264 291
229 264 291
232 265 260
260 265 292
232 265 292
230 266 262
262 266 292
230 266 292
233 267 270
270 267 293
233 267 293
234 268 272
234 268 294
272 268 294
235 269 273
235 269 293
273 269 293
237 270 267
267 270 295
237 270 295
236 271 274
236 271 294
274 271 294
238 272 268
268 272 296
238 272 296
243 273 269
269 273 295
243 273 295
244 274 271
271 274 296
244 274 296
239 275 277
277 275 297
239 275 297
240 276 278
278 276 298
240 276 298
241 277 275
275 277 299
241 277 299
242 278 276
276 278 300
242 278 300
245 279 281
245 279 297
281 279 297
246 280 282
282 280 298
246 280 298
247 281 279
279 281 299
247 281 299
248 282 280
280 282 300
248 282 300
252 283 285
142 283 252
142 283 285
253 284 286
145 284 253
145 284 286
255 285 283
249 285 283
249 285 255
256 286 284
250 286 284
250 286 256
251 287 257
257 287 289
251 287 289
258 288 290
254 288 290
254 288 258
259 289 287
261 289 287
259 289 261
262 290 288
260 290 288
260 290 262
263 291 264
263 291 293
264 291 293
265 292 266
266 292 294
265 292 294
267 293 291
269 293 291
267 293 269
268 294 292
271 294 292
268 294 271
270 295 273
273 295 297
270 295 297
274 296 298
272 296 298
272 296 274
279 297 295
275 297 295
275 297 279
276 298 296
280 298 296
276 298 280
281 299 300
277 299 300
277 299 281
278 300 299
282 300 299
278 300 282
Here is a picture of the weird output I have been getting. (The output of sums of 2 and 4, when I want sum of 3.) (Also Note there is more than one instance of these sums of 2 and 4 appearing.)
The format of the file looks a bit weird and surely redundant (even without considering the symmetry of the resulting matrix).
The choice of a 2D array of ints to represent a symmetric sparse (1% of non-zero elements, all of value 1) matrix may be a waste of space too, but the question doesn't specify the underlying problem, so I will not speculate any further.
It seems that in the input file rows and column are indexed starting by 1, while in C++ array indeces start by 0, so when a line like
281 299 300
is read by OP's code:
stream >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
The value 300 is off by one and being 2D arrays contiguous in memory, instead of the last element of the row (the second value represent the row number, apparently) the first element of next row is set to 1.
Given a well formatted input file, like the one presented, is also easier to read the values directly from the file stream using operator>>, without using a string stream.
The same snippet can be rewritten and "simplified" like this:
#include <iostream>
#include <fstream>
template <size_t Dim>
bool is_symmetric(int (&arr)[Dim][Dim]);
template <size_t Dim>
bool are_there_exactly_3_nonzeroes_each_row(int (&arr)[Dim][Dim]);
template <size_t Dim>
int nonzeroes_on_diagonal(int (&arr)[Dim][Dim]);
const size_t size = 300;
int main()
{
// Declares a 2D array of 300 by 300 int and initializes it to zero:
int adMatrix [size][size] = {};
std::ifstream infile("myTestFile.txt");
if ( !infile )
{
std::cerr << "Unable to open input file.\n";
return EXIT_FAILURE;
}
// The value in the second column is the line number.
// All the the values in the file are indeces in the range 1 - 300
int first_col, row, second_col;
// This will read all the file, if it is well formatted.
while ( infile >> first_col >> row >> second_col )
{
if ( first_col < 1 or first_col > size
or row < 1 or row > size
or second_col < 1 or second_col > size )
{
std::cerr << "Wrong file format.\n The line with data: "
<< first_col << ' ' << row << ' ' << second_col
<< " have out of range indeces.\n";
return EXIT_FAILURE;
}
adMatrix[row - 1][first_col - 1] = 1;
adMatrix[row - 1][second_col - 1] = 1;
}
// Print values or ...
if ( is_symmetric(adMatrix) == false )
{
std::cerr << "The matrix isn't symmetric.\n";
}
if ( are_there_exactly_3_nonzeroes_each_row(adMatrix) == false )
{
std::cerr << "There aren't exactly three non zero elements each row.\n";
}
std::cout << "Number of non zero elements in the main diagonal: "
<< nonzeroes_on_diagonal(adMatrix) << '\n';
}
template <size_t Dim>
bool is_symmetric(int (&arr)[Dim][Dim])
{
for ( size_t i = 0; i < Dim; ++i )
{
for ( size_t j = i + 1; j < Dim; ++j )
{
if ( arr[i][j] != arr[j][i] )
return false;
}
}
return true;
}
template <size_t Dim>
bool are_there_exactly_3_nonzeroes_each_row(int (&arr)[Dim][Dim])
{
for ( size_t i = 0; i < Dim; ++i )
{
int count = 0;
for ( size_t j = 0; j < Dim; ++j )
{
if ( arr[i][j] != 0 )
++count;
}
if ( count != 3 )
return false;
}
return true;
}
template <size_t Dim>
int nonzeroes_on_diagonal(int (&arr)[Dim][Dim])
{
int count = 0;
for ( size_t i = 0; i < Dim; ++i )
{
if ( arr[i][i] != 0 )
{
++count;
}
}
return count;
}
This will fix the issue, but for a more general solution and to really improve your language knowledge it would be far more useful to start learning about classes, the C++ Standard Library, its containers and generic functions.
I am currently a student working on an insertion sort method.
Below is the code:
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i < INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
In my tests, I have given it the array from left to right:
307 249 73 158 430 272 44 378 423 209
440 165 492 42 487 3 327 229 340 112
303 169 209 157 60 433 99 278 316 335
97 326 12 267 310 133 479 149 79 321
467 172 393 336 485 245 228 91 194 357
1 153 208 444 168 490 124 196 30 403
222 166 49 24 301 353 477 408 228 433
298 481 135 13 365 314 63 36 425 169
115 94 129 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
The method produces from left to right:
314 63 314 63 36 425 36 169 425 169
115 115 94 129 94 129 1 17 195 105
404 451 298 188 123 5 382 252 66 216
337 438 144 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
228 229 245 249 252 267 272 278 298 298
301 303 307 310 314 316 321 326 327 335
336 337 340 353 357 365 378 382 393 403
404 408 423 425 430 433 433 438 440 444
451 467 477 479 481 485 487 490 492 144
What am I incorrectly coding?
Thanks!
EDIT:
//In main...
Printing(insertionSortValues, "Insertion Sorted Array");
//Function for Print
void Printing(int vals[], string s){
cout << s << ":" << endl;
for(int i = 0; i < INITSIZE; i++){
if(i % 10 == 0){
cout << endl;
}
cout << setw(3) << vals[i] << " ";
}
cout << endl;
}
The solution to this problem was solved through #PaulMcKenzie.
The line:
for(int i = 0; i < INITSIZE - 1; i++){
needed to become:
for(int i = 0; i <= INITSIZE - 1; i++){
Below is the corrected function.
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i <= INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
Apologies for the long code. This is as far as I could reduce it.
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <vector>
using namespace std;
class View : public QWidget {
typedef pair<double, double> Point;
unsigned char* _buffer;
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
public:
View(int w, int h) : width(w), height(h) {
_buffer = new unsigned char[4 * w * h];
fwidth = static_cast<double>(width);
fheight = static_cast<double>(height);
double aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return std::floor(x / xprec) * xprec; }
double roundY(double y) { return std::floor(y / yprec) * yprec; }
protected:
void paintEvent(QPaintEvent* event) {
QPainter painter(this);
render();
painter.drawImage(
QPoint(0, 0),
QImage(_buffer, width, height, QImage::Format_RGB32));
}
private:
void render() {
memset(_buffer, 0, 4 * width * height);
for (double i = xmin; i < xmax; i += xprec) {
for (double j = ymin; j < ymax; j += yprec) {
Point p(roundX(i), roundY(j));
int x = static_cast<int>((p.first * xratio) - (xmin * xratio) );
int y = static_cast<int>((p.second * yratio) - (ymin * yratio) );
_buffer[4 * (x * width + y) ] = 255;
_buffer[4 * (x * width + y) + 1] = 255;
_buffer[4 * (x * width + y) + 2] = 255;
}
}
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
View view(512, 512);
view.show();
return app.exec();
}
The code, instead of producing a white window, produces a white window with lines which are the result of round-off error. I think the source of the problem are roundX() and roundY() functions, but I'm not sure. I also don't know how to fix this. Any ideas?
I don't have Qt and I prefer C. So, below is a C program that imitates your render() in render1() and has a corrected implementation in render2().
The main problem of your render() is that it uses prematurely rounded values from Point p(roundX(i), roundY(j));. If you use unrounded i and j, things will improve dramatically, but you're still going to suffer from the rounding errors of your additions, multiplications and so on.
But those rounding errors aren't large and they don't accumulate to a large error in the end. So, the final correction is the addition of .5 prior to conversion to the integers x and y. Without it, the floating point value can be slightly less than the nearest integer that you want to get and when you convert this double into int, you get a value that is one less.
#include <stdio.h>
#include <string.h>
#include <math.h>
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
void InitView(int w, int h)
{
double aspectRatio;
width = w; height = h;
// _buffer = new unsigned char[4 * w * h];
fwidth = width;
fheight = height;
aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return floor(x / xprec) * xprec; }
double roundY(double y) { return floor(y / yprec) * yprec; }
void render1(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render1():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((roundX(i) * xratio) - (xmin * xratio));
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((roundY(j) * yratio) - (ymin * yratio));
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
void render2(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render2():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((i * xratio) - (xmin * xratio) + .5);
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((j * yratio) - (ymin * yratio) + .5);
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
int main(void)
{
InitView(512, 512);
render1();
render2();
return 0;
}
Output (ideone):
render1():
x's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing x: 1
missing x: 3
missing x: 6
missing x: 8
missing x: 11
missing x: 13
missing x: 16
missing x: 18
missing x: 21
missing x: 23
missing x: 26
missing x: 28
missing x: 31
missing x: 34
missing x: 39
missing x: 44
missing x: 49
missing x: 53
missing x: 58
missing x: 63
missing x: 68
missing x: 73
missing x: 78
missing x: 83
missing x: 88
missing x: 93
missing x: 98
missing x: 102
missing x: 107
missing x: 112
missing x: 117
missing x: 122
missing x: 127
missing x: 132
missing x: 137
missing x: 142
missing x: 147
missing x: 149
missing x: 154
missing x: 159
missing x: 161
missing x: 164
missing x: 169
missing x: 176
missing x: 181
missing x: 186
missing x: 191
missing x: 196
missing x: 372
missing x: 377
missing x: 382
missing x: 387
missing x: 392
missing x: 421
missing x: 426
missing x: 431
missing x: 436
missing x: 441
missing x: 480
missing x: 483
missing x: 485
missing x: 488
missing x: 490
missing x: 493
missing x: 498
missing x: 503
missing x: 508
y's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing y: 1
missing y: 3
missing y: 6
missing y: 8
missing y: 11
missing y: 13
missing y: 16
missing y: 18
missing y: 21
missing y: 23
missing y: 26
missing y: 28
missing y: 31
missing y: 34
missing y: 39
missing y: 44
missing y: 49
missing y: 53
missing y: 58
missing y: 63
missing y: 68
missing y: 73
missing y: 78
missing y: 83
missing y: 88
missing y: 93
missing y: 98
missing y: 102
missing y: 107
missing y: 112
missing y: 117
missing y: 122
missing y: 127
missing y: 132
missing y: 137
missing y: 142
missing y: 147
missing y: 149
missing y: 154
missing y: 159
missing y: 161
missing y: 164
missing y: 169
missing y: 176
missing y: 181
missing y: 186
missing y: 191
missing y: 196
missing y: 372
missing y: 377
missing y: 382
missing y: 387
missing y: 392
missing y: 421
missing y: 426
missing y: 431
missing y: 436
missing y: 441
missing y: 480
missing y: 483
missing y: 485
missing y: 488
missing y: 490
missing y: 493
missing y: 498
missing y: 503
missing y: 508
render2():
x's:
0 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512
y's:
0 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512