Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
111
Task/Atomic-updates/C/atomic-updates-1.c
Normal file
111
Task/Atomic-updates/C/atomic-updates-1.c
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#define N_BUCKETS 15
|
||||
|
||||
pthread_mutex_t bucket_mutex[N_BUCKETS];
|
||||
int buckets[N_BUCKETS];
|
||||
|
||||
pthread_t equalizer;
|
||||
pthread_t randomizer;
|
||||
|
||||
void transfer_value(int from, int to, int howmuch)
|
||||
{
|
||||
bool swapped = false;
|
||||
|
||||
if ( (from == to) || ( howmuch < 0 ) ||
|
||||
(from < 0 ) || (to < 0) || (from >= N_BUCKETS) || (to >= N_BUCKETS) ) return;
|
||||
|
||||
if ( from > to ) {
|
||||
int temp1 = from;
|
||||
from = to;
|
||||
to = temp1;
|
||||
swapped = true;
|
||||
howmuch = -howmuch;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&bucket_mutex[from]);
|
||||
pthread_mutex_lock(&bucket_mutex[to]);
|
||||
|
||||
if ( howmuch > buckets[from] && !swapped )
|
||||
howmuch = buckets[from];
|
||||
if ( -howmuch > buckets[to] && swapped )
|
||||
howmuch = -buckets[to];
|
||||
|
||||
buckets[from] -= howmuch;
|
||||
buckets[to] += howmuch;
|
||||
|
||||
pthread_mutex_unlock(&bucket_mutex[from]);
|
||||
pthread_mutex_unlock(&bucket_mutex[to]);
|
||||
}
|
||||
|
||||
void print_buckets()
|
||||
{
|
||||
int i;
|
||||
int sum=0;
|
||||
|
||||
for(i=0; i < N_BUCKETS; i++) pthread_mutex_lock(&bucket_mutex[i]);
|
||||
for(i=0; i < N_BUCKETS; i++) {
|
||||
printf("%3d ", buckets[i]);
|
||||
sum += buckets[i];
|
||||
}
|
||||
printf("= %d\n", sum);
|
||||
for(i=0; i < N_BUCKETS; i++) pthread_mutex_unlock(&bucket_mutex[i]);
|
||||
}
|
||||
|
||||
void *equalizer_start(void *t)
|
||||
{
|
||||
for(;;) {
|
||||
int b1 = rand()%N_BUCKETS;
|
||||
int b2 = rand()%N_BUCKETS;
|
||||
int diff = buckets[b1] - buckets[b2];
|
||||
if ( diff < 0 )
|
||||
transfer_value(b2, b1, -diff/2);
|
||||
else
|
||||
transfer_value(b1, b2, diff/2);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *randomizer_start(void *t)
|
||||
{
|
||||
for(;;) {
|
||||
int b1 = rand()%N_BUCKETS;
|
||||
int b2 = rand()%N_BUCKETS;
|
||||
int diff = rand()%(buckets[b1]+1);
|
||||
transfer_value(b1, b2, diff);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, total=0;
|
||||
|
||||
for(i=0; i < N_BUCKETS; i++) pthread_mutex_init(&bucket_mutex[i], NULL);
|
||||
|
||||
for(i=0; i < N_BUCKETS; i++) {
|
||||
buckets[i] = rand() % 100;
|
||||
total += buckets[i];
|
||||
printf("%3d ", buckets[i]);
|
||||
}
|
||||
printf("= %d\n", total);
|
||||
|
||||
// we should check if these succeeded
|
||||
pthread_create(&equalizer, NULL, equalizer_start, NULL);
|
||||
pthread_create(&randomizer, NULL, randomizer_start, NULL);
|
||||
|
||||
for(;;) {
|
||||
sleep(1);
|
||||
print_buckets();
|
||||
}
|
||||
|
||||
// we do not provide a "good" way to stop this run, so the following
|
||||
// is never reached indeed...
|
||||
for(i=0; i < N_BUCKETS; i++) pthread_mutex_destroy(bucket_mutex+i);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
51
Task/Atomic-updates/C/atomic-updates-2.c
Normal file
51
Task/Atomic-updates/C/atomic-updates-2.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <omp.h>
|
||||
|
||||
#define irand(n) (n * (double)rand()/(RAND_MAX + 1.0))
|
||||
|
||||
int bucket[10];
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 10; i++) bucket[i] = 1000;
|
||||
omp_set_num_threads(3);
|
||||
|
||||
#pragma omp parallel private(i)
|
||||
for (i = 0; i < 10000; i++) {
|
||||
int from, to, mode, diff = 0, sum;
|
||||
|
||||
from = irand(10);
|
||||
do { to = irand(10); } while (from == to);
|
||||
mode = irand(10);
|
||||
|
||||
switch (mode) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2: /* equalize */
|
||||
diff = (bucket[from] - bucket[to]) / 2;
|
||||
break;
|
||||
|
||||
case 3: /* report */
|
||||
sum = 0;
|
||||
for (int j = 0; j < 10; j++) {
|
||||
printf("%d ", bucket[j]);
|
||||
sum += bucket[j];
|
||||
}
|
||||
printf(" Sum: %d\n", sum);
|
||||
continue;
|
||||
|
||||
default: /* random transfer */
|
||||
diff = irand(bucket[from]);
|
||||
break;
|
||||
}
|
||||
|
||||
#pragma omp critical
|
||||
{
|
||||
bucket[from] -= diff;
|
||||
bucket[to] += diff;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
Task/Atomic-updates/C/atomic-updates-3.c
Normal file
8
Task/Atomic-updates/C/atomic-updates-3.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1000 1000 1000 1798 1000 1000 1000 1000 202 1000 Sum: 10000
|
||||
595 800 2508 2750 470 1209 283 314 601 470 Sum: 10000
|
||||
5 521 3339 1656 351 1038 1656 54 508 872 Sum: 10000
|
||||
.
|
||||
.
|
||||
.
|
||||
752 490 385 2118 1503 508 384 509 1110 2241 Sum: 10000
|
||||
752 823 385 2118 1544 508 10 509 1110 2241 Sum: 10000
|
||||
Loading…
Add table
Add a link
Reference in a new issue