Created
January 12, 2012 19:34
-
-
Save ciembor/1602561 to your computer and use it in GitHub Desktop.
PRIR 04 - UPC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* autor: Maciej Ciemborowicz | |
| * PRIR 2011/2012 | |
| * bitwaMorska | |
| * testowane z GCC UPC 4.7.0 | |
| */ | |
| #include <upc_relaxed.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| #define SIZE 100000 | |
| #define SHIPS 5000 | |
| enum states { EMPTY, SHIP, HIT }; | |
| upc_lock_t *lock; | |
| shared [ ] int table [ SIZE ]; /* plansza do gry */ | |
| shared [ ] int points [ THREADS ]; /* ilość zatopionych przez gracza statków */ | |
| shared int ships = SHIPS; /* ilość niezatopionych statków */ | |
| shared int end = 0; | |
| void show( int i ) { | |
| switch( i ) { | |
| case 0: printf( "_____ " ); break; /* morze */ | |
| case 1: printf( "__^__ " ); break; /* statek */ | |
| case 2: printf( "_***_ " ); break; /* trafiony statek */ | |
| } | |
| } | |
| int main(){ | |
| int i; | |
| /* randomizacja (dla każdego wątku osobno) */ | |
| srandom((MYTHREAD+1) * 123 * time(0)); | |
| /* losowanie statków, zrównoleglanie tego nie ma sensu | |
| * (jedna wspólna plansza, jak w specyfikacji) | |
| */ | |
| if ( 0 == MYTHREAD ) { | |
| for ( i = 0; i < SIZE; i++ ) { | |
| table[ i ] = EMPTY; | |
| } | |
| int ships = 0; | |
| int idx; | |
| do { | |
| idx = (int)( SIZE * (double)random() / (double)RAND_MAX ); | |
| if ( table[ idx ] == EMPTY ) { | |
| table[ idx ] = SHIP; | |
| ships++; | |
| } | |
| } while ( ships < SHIPS ); | |
| } | |
| upc_barrier; /* statki są ustawione przed rozpoczęciem gry */ | |
| /* inicjalizacja punktów gracza (wątku) */ | |
| points[ MYTHREAD ] = 0; | |
| /* ustawienie zamka */ | |
| if ( ( lock=upc_all_lock_alloc()) == 0 ) | |
| upc_global_exit(1); | |
| /* właściwa gra - strzelanie do statków */ | |
| int idx; | |
| do { | |
| idx = (int)( SIZE * (double)random() / (double)RAND_MAX ); /* losuję pole */ | |
| if ( table[ idx ] == SHIP ) { /* statek trafiony */ | |
| upc_lock(lock); /* zablokuj pamięć */ | |
| table[ idx ] = HIT; /* zmień statek na zatopiony */ | |
| ships -= 1; /* dekrementacja ilości statków */ | |
| upc_unlock(lock); /* odblokuj pamięć */ | |
| points[ MYTHREAD ] += 1; /* inkrementacja punktów wątku */ | |
| if ( 0 >= ships || points[ MYTHREAD ] > (float)SHIPS/2.0 ) { | |
| end = 1; /* koniec gry */ | |
| } | |
| } | |
| upc_barrier; /* synchronizacja gry (tura) */ | |
| } while ( !end ); | |
| upc_barrier; | |
| /* wyniki mają być posortowane (ranking), | |
| * więc tylko jeden proces wyswietla wyniki | |
| */ | |
| if ( 0 == MYTHREAD ) { | |
| upc_lock_free(lock); | |
| int sum = 0; | |
| int max = SHIPS; | |
| int j; | |
| int thread = -1; | |
| int last = -1; | |
| int last_thread = -1; | |
| for ( j = 0; j < THREADS; ++j ) { | |
| last = -1; | |
| for ( i = 0; i < THREADS; ++i ) { | |
| if ((points[i] > last && points[i] < max)) { | |
| thread = i; | |
| last = points[i]; | |
| } | |
| else if ( (last_thread < i) && (max == points[i]) && (points[i] == points[last_thread])) { | |
| thread = i; | |
| last = points[i]; | |
| break; | |
| } | |
| } | |
| max = last; | |
| last_thread = thread; | |
| printf("place: %d\tthread: %d\t%d points\n", j, thread, points[thread]); | |
| sum += points[thread]; | |
| } | |
| printf("=======================================================\n"); | |
| printf("sunken ships: %d of %d\n", sum, SHIPS); | |
| upc_global_exit(0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment