summaryrefslogtreecommitdiffstats
path: root/mat/programčki/ničle.mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'mat/programčki/ničle.mutex.c')
-rw-r--r--mat/programčki/ničle.mutex.c105
1 files changed, 0 insertions, 105 deletions
diff --git a/mat/programčki/ničle.mutex.c b/mat/programčki/ničle.mutex.c
deleted file mode 100644
index cf453d8..0000000
--- a/mat/programčki/ničle.mutex.c
+++ /dev/null
@@ -1,105 +0,0 @@
-#include <stdlib.h> // navdih za to je tedx predstavitev andreja bauerja z naslovom ničle
-#include <stdio.h> // to je izris ničel littlewoodovih polinomov
-#include <gsl/gsl_poly.h> // prevod: gcc -Wall -Wextra ničle.c -lgsl -lm -pthread
-#include <sys/types.h> // program na poti "ime" izdela kvadratno PGM datoteko podane višine
-#include <sys/stat.h> // za nadaljno obdelavo je uporabno, če je širina liha; ni pa nujno
-#include <fcntl.h> // algoritem se da paralelizirati, ampak jaz imam samo en procesor
-#include <sys/mman.h> // enotska krožnica je na četrtini podane širine
-#include <string.h>
-#include <unistd.h>
-#include <gsl/gsl_errno.h>
-#include <pthread.h>
-struct nit {
- int začetek;
- int konec; // nit, pomni, da računaš DO konca in konca ne računaš; hvala
-};
-void pripravi_koeficiente (double * izhod, int številka) {
- while (številka) {
- *izhod++ = številka & 1 ? 1 : -1;
- številka >>= 1;
- }
-}
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-_Atomic int izvs = 0, nekonvergiranih = 0, stopnja, ši;
-unsigned char * slika;
-void * računaj (void * vhod) {
- double koeficienti[stopnja+1]; // kako prikladno! polinom nte stopnje ima n+1 členov
- double ničle[2*stopnja]; // ima pa n ničer, 2n+0 so realni deli, 2n+1 pa imaginarni
- struct nit * nit = (struct nit *) vhod;
- gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc(stopnja+1);
- for (int i = nit->začetek; i < nit->konec; i++) {
- pripravi_koeficiente(koeficienti, i); // noben člen ni 0, vsi so bodisi 1 bodisi -1
- if (gsl_poly_complex_solve(koeficienti, stopnja+1, w, ničle) != GSL_SUCCESS)
- nekonvergiranih++; // uuu, lahko bi recimo narisali tiste, ki ne konver.
- for (int j = 0; j < 2*stopnja; j += 2) {
- int višs = ši/2 - ničle[j+1]*(ši/4);
- int širs = ši/2 + ničle[j]*(ši/4);
- if (višs > ši || širs > ši || višs < 0 || širs < 0) {
- izvs++;
- continue;
- }
- pthread_mutex_lock(&mutex);
- slika[2*ši*višs+širs*2+1]++;
- if (!slika[2*ši*višs+širs*2+1])
- if (slika[2*ši*višs+širs*2] != 255)
- slika[2*ši*višs+širs*2]++;
- pthread_mutex_unlock(&mutex);
- }
- }
- gsl_poly_complex_workspace_free(w);
- free(nit); return NULL;
-}
-int main (int argc, char ** argv) {
- if (argc != 1+4) {
- fprintf(stderr, "takole: %s stopnja ime širina niti\n", argv[0] ? argv[0] : "ničle");
- return 1;
- }
- int r = 0, kje_začeti = 0, fd, šn = atoi(argv[4]);
- stopnja = atoi(argv[1]);
- ši = atoi(argv[3]);
- pthread_t niti[šn];
- if ((fd = open(argv[2], O_CREAT | O_RDWR, 00664)) == -1) {
- perror("open");
- return 2;
- }
- if (ftruncate(fd, 128 + ši*ši*2) == -1) {
- perror("ftruncate");
- if (close(fd) == -1)
- perror("close");
- return 3;
- }
- void * p;
- if ((p = mmap(NULL, 128 + ši*ši*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
- perror("mmap");
- if (close(fd) == -1)
- perror("close");
- return 4;
- }
- slika = (unsigned char *) p + 128;
- memset(p, 0, 128 + ši*ši*2);
- sprintf(p, "P5\n\n%58d\n%58d\n65535\n", ši, ši); // precisely calculated with dc(1) (:
- gsl_set_error_handler_off();
- for (int i = 0; i < šn; i++) {
- struct nit * nit = malloc(sizeof(struct nit));
- nit->začetek = kje_začeti;
- nit->konec = (kje_začeti += ((1 << (stopnja+1))-1) / šn);
-
- if ((r = pthread_create(niti+i, NULL, računaj, nit))) {
- fprintf(stderr, "pthread_create: %s (%d)\n", strerror(r), r);
- r = 5; goto r;
- }
- }
- for (int i = 0; i < šn; i++) {
- if ((r = pthread_join(niti[i], NULL))) {
- fprintf(stderr, "pthread_join: %s (%d)\n", strerror(r), r);
- r = 6; goto r;
- }
- }
-r:
- if (munmap(p, 128 + ši*ši*2) == -1)
- perror("munmap"); // nima smisla ukinjat programa, sistem si je sam kriv >:)
- if (close(fd) == -1)
- perror("close");
- printf("%d ničel je izven 2+2i\n%d polinomov ni konvergiralo\n", izvs, nekonvergiranih);
- return r;
-}