Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
range
a.out
*.o
11 changes: 0 additions & 11 deletions MAKE.SH

This file was deleted.

9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
all:
gcc range.c -o range -lpthread
run:
./range
grind:
gcc range.c -o range -l pthread
valgrind ./range
optimized:
gcc -O3 range.c -o range -lpthread
60 changes: 50 additions & 10 deletions range.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/sysinfo.h>

/* Размер массива */
#define LIST_SIZE 100000
Expand All @@ -17,12 +19,16 @@ unsigned int father[LIST_SIZE];
struct list_head *pair_list;
/* Тестовый автоматик */
static unsigned int ar[qwerty][erty];
static int cpu_count;
static int thread_state[128];
static pthread_t thread[128];
static int iterator = 0;

/* Описание структуры состояния*/
struct state {
int q;
struct list_head list;
};
};

/* Описание структуры для "использованных" пар */
struct pair {
Expand All @@ -31,6 +37,12 @@ struct pair {
struct list_head list;
};

/* Возвращает количество ядер процессоров */
int get_cpu_count()
{
return get_nprocs();
}

/* печатает все элементы массива */
void state_list_print() {
unsigned int i;
Expand Down Expand Up @@ -268,13 +280,12 @@ int range() {
stuff = 1; //на последнее не пустое множество в массиве

/* Now, MAGIC! */

mark:
while(current_set != 0) {
flag = 0;
for (k=0; k<erty; k++) {
for (i=0; i<qwerty; i++) { // обходим по парам
if(pair_approved(k,current_set)) {
for (k=0; k<erty; k++)
for (i=0; i<qwerty; i++) // обходим по парам
if(pair_approved(k,current_set))
if(new_state_set(current_set, stuff, k)) {
stuff++;
pair_add(k,current_set);
Expand All @@ -283,12 +294,8 @@ int range() {
flag = 1;
goto mark;
}
}
}
}
if(!flag) {
if(!flag)
current_set = father[current_set];
}
}

for(i=1; i<LIST_SIZE; i++)
Expand All @@ -297,6 +304,7 @@ int range() {
range = sizeof_set(i);
return range;
}

/* Генерирует случайный автомат*/
void ar_random() {
unsigned int i,j;
Expand Down Expand Up @@ -327,15 +335,47 @@ void print_automat() {
}
printf("\n");
}

void *worker(int cpu_id)
{
thread_state[cpu_id] = 1;
iterator++;
init();
ar_random();
printf("%d\n", range());
cleanup();
thread_state[cpu_id] = 0;
return;
}

void *controller()
{
int i;
for (i = 0; i < cpu_count-1; i++)
pthread_create(&thread[i], NULL, worker(i), NULL);
while (1)
for (i = 0; i < cpu_count-1; i++)
if (thread_state[i] == 0)
pthread_join(thread[i], NULL);
return;
}

int main() {
unsigned int i;
cpu_count = get_cpu_count();
pthread_t main_controller;
srand(time(NULL));
printf("Segfault because usage of one array by all threads, just give them their own copy\n");
pthread_create(&main_controller, NULL, controller, NULL);
pthread_join(main_controller, NULL);
#if 0
for (i = 0; i < 1000000; i++) {
init();
ar_random();
printf("%d\n", range());
cleanup();
}
#endif

return 0;
}
Expand Down