1 /* Epiphany Host Application */
11 /* common data structures */
12 #include "../shared.h"
14 #define FAIL(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
15 #define SHM_OFFSET 0x01000000
20 #define NUM_PROGRAMS 2
23 char programs[NUM_PROGRAMS][64] = {
28 /* local copy of shared memory */
32 static uint32_t xorshift32(void)
34 static uint32_t x = 314159265;
54 int main(int argc, char *argv[])
56 char filename[255]; /* filename to load */
59 int32_t states[NUM_CORES] = { 0 };
60 int32_t laststates[NUM_CORES] = { 0 };
65 e_set_host_verbosity(H_D0);
66 e_set_loader_verbosity(L_D0);
68 /* init board, reset epiphany, open workgroup */
69 if(e_init(NULL) != E_OK)
70 FAIL("Can't init!\n");
72 if(e_open(&dev, 0, 0, 4, 4) != E_OK)
73 FAIL("Can't open!\n");
75 /* initialize, allocate and update shared memory buffer */
77 if(e_alloc(&emem, SHM_OFFSET, sizeof(shm_t)) != E_OK)
78 FAIL("Can't alloc!\n");
79 if(e_write(&emem, 0, 0, (off_t)0, &shm, sizeof(shm_t)) == E_ERR)
80 FAIL("Can't clear shm!\n");
81 if(e_write(&emem, 0, 0, (off_t)0, &states, sizeof(states)) == E_ERR)
82 FAIL("Can't clear states!\n");
84 /* load and start all programs */
85 for(int i = 0; i < NUM_PROGRAMS; i++) {
86 snprintf(filename, 255, "%s", programs[i]);
87 printf("Kicking core %i = (%i, %i) with '%s'\n",
88 i, ROW(i), COL(i), filename);
89 if(e_load(filename, &dev, ROW(i), COL(i), E_TRUE) != E_OK)
90 FAIL("Can't load %i!\n", i);
93 /* =============================================================== */
94 printf("Started %i cores.\n", NUM_PROGRAMS);
98 /* poll shm states for changes */
99 printf("Polling shared memory.\n");
102 /* read epiphany core states */
103 if(e_read(&emem, 0, 0, (off_t)0, &states,
104 sizeof(states)) == E_ERR)
105 FAIL("Can't poll!\n");
107 /* check for state changes */
108 if(memcmp(laststates, states, sizeof(states)))
112 /* save current states */
113 memcpy(laststates, states, sizeof(states));
115 /* print core states */
116 for(int i = 0; i < NUM_PROGRAMS; i++) {
117 printf("CORE %i: ", i);
119 case -1: printf("done "); break;
120 default: printf("state %i ", states[i]); done=0; break;
124 printf("CORE14: 0x%x\n", states[14]);
125 printf("CORE15: 0x%x\n", states[15]);
127 /* stop polling if all cores finished */
130 /* =============================================================== */
132 printf("All cores finished.\n");
134 /* read whole shm buffer */
135 if(e_read(&emem, 0, 0, (off_t)0, &shm, sizeof(shm)) == E_ERR)
136 FAIL("Can't read full shm!\n");
138 /* print buffer 0 and buffer 1 */
139 for(int i = 0; i < 16; i++) {
140 printf("0x%x ", shm.buf0.buf[i]);
142 for(int i = 0; i < 16; i++) {
143 printf("0x%x ", shm.buf1.buf[i]);
147 for(int i = 0; i < 4; i++) {
148 printf("%.2f\t", ((float*)shm.buf0.buf)[i]);
151 for(int i = 0; i < 8; i++) {
152 printf("%.2f\t", ((float*)shm.buf1.buf)[i]);
155 /* free shm buffer, close workgroup and finalize */
156 if(e_free(&emem) != E_OK)
157 FAIL("Can't free!\n");
158 if(e_close(&dev) != E_OK)
159 FAIL("Can't close!\n");
160 if(e_finalize() != E_OK)
161 FAIL("Can't finalize!\n");