Skip to content
Snippets Groups Projects
Verified Commit a002f134 authored by Dorian Stoll's avatar Dorian Stoll
Browse files

zellularautomat: c: Merge program into one C file

parent f07d4a0c
No related branches found
No related tags found
No related merge requests found
/*
* common functions for (parallel) cellular automaton
*
* (c) 2016 Steffen Christgau
*
* configuration initialization based on
* (c) 1996,1997 Peter Sanders, Ingo Boesnach
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "ca_common.h"
void ca_init(int argc, char** argv, int *lines, int *its)
{
assert(argc == 3);
*lines = atoi(argv[1]);
*its = atoi(argv[2]);
assert(*lines > 0);
}
/* random starting configuration */
void ca_init_config(line_t *buf, int lines)
{
srand(424243);
for (int y = 1; y <= lines; y++) {
for (int x = 1; x <= XSIZE; x++) {
buf[y][x] = rand() % 2;
}
}
}
static void ca_clean_ghost_zones(line_t *buf, int lines)
{
for (int y = 0; y < lines; y++) {
buf[y][0] = 0;
buf[y][XSIZE + 1] = 0;
}
}
void ca_report(line_t *buf, int lines)
{
ca_clean_ghost_zones(buf, lines);
fwrite(buf, sizeof(*buf), lines, stdout);
}
#ifndef CA_COMMON_H
#define CA_COMMON_H
#include <stdint.h>
#include <stddef.h>
/* a: pointer to array; x,y: coordinates; result: n-th element of anneal,
where n is the number of neighbors */
#define transition(a, x, y) \
(anneal[(a)[(y)-1][(x)-1] + (a)[(y)][(x)-1] + (a)[(y)+1][(x)-1] +\
(a)[(y)-1][(x) ] + (a)[(y)][(x) ] + (a)[(y)+1][(x) ] +\
(a)[(y)-1][(x)+1] + (a)[(y)][(x)+1] + (a)[(y)+1][(x)+1]])
#ifdef __cplusplus
extern "C" {
#endif
/* horizontal size of the configuration */
#define XSIZE 1024
#define LINE_SIZE (XSIZE + 2)
/* "ADT" State and line of states (plus border) */
typedef uint8_t cell_state_t;
typedef cell_state_t line_t[XSIZE + 2];
void ca_init(int argc, char** argv, int *lines, int *its);
void ca_init_config(line_t *buf, int lines);
void ca_report(line_t *buf, int lines);
#ifdef __cplusplus
}
#endif
#endif /* CA_COMMON_H */
...@@ -10,10 +10,60 @@ ...@@ -10,10 +10,60 @@
* #2: Number of iterations to be simulated * #2: Number of iterations to be simulated
* *
*/ */
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
/* a: pointer to array; x,y: coordinates; result: n-th element of anneal,
where n is the number of neighbors */
#define transition(a, x, y) \
(anneal[(a)[(y)-1][(x)-1] + (a)[(y)][(x)-1] + (a)[(y)+1][(x)-1] +\
(a)[(y)-1][(x) ] + (a)[(y)][(x) ] + (a)[(y)+1][(x) ] +\
(a)[(y)-1][(x)+1] + (a)[(y)][(x)+1] + (a)[(y)+1][(x)+1]])
/* horizontal size of the configuration */
#define XSIZE 1024
#define LINE_SIZE (XSIZE + 2)
/* "ADT" State and line of states (plus border) */
typedef uint8_t cell_state_t;
typedef cell_state_t line_t[XSIZE + 2];
/* ------------------------- CA utils -------------------------------- */
static void ca_init(int argc, char** argv, int *lines, int *its)
{
assert(argc == 3);
*lines = atoi(argv[1]);
*its = atoi(argv[2]);
assert(*lines > 0);
}
#include "ca_common.h" /* random starting configuration */
static void ca_init_config(line_t *buf, int lines)
{
srand(424243);
for (int y = 1; y <= lines; y++) {
for (int x = 1; x <= XSIZE; x++) {
buf[y][x] = rand() % 2;
}
}
}
static void ca_report(line_t *buf, int lines)
{
for (int y = 0; y < lines; y++) {
buf[y][0] = 0;
buf[y][XSIZE + 1] = 0;
}
fwrite(buf, sizeof(*buf), lines, stdout);
}
/* --------------------- CA simulation -------------------------------- */ /* --------------------- CA simulation -------------------------------- */
......
sources = [ sources = [
'ca_common.c', 'main.c',
'ca_openmp.c',
] ]
dependencies = [ dependencies = [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment