int net_init(struct socket *s) { int i = s - sockets; int n = net_alloc(); if (n < 0) { udata.u_error = ENOENT;//FIXME ENOBUFS; return -1; } wiz2sock_map[n] = i; sock2wiz_map[i] = n; s->s_state = SS_UNCONNECTED; return 0; }
/* ============================================================================= * data_generate * -- Binary variables of random PDFs * -- If seed is <0, do not reseed * -- Returns random network * ============================================================================= */ net_t* data_generate (data_t* dataPtr, long seed, long maxNumParent, long percentParent) { random_t* randomPtr = dataPtr->randomPtr; if (seed >= 0) { random_seed(randomPtr, seed); } /* * Generate random Bayesian network */ long numVar = dataPtr->numVar; net_t* netPtr = net_alloc(numVar); assert(netPtr); net_generateRandomEdges(netPtr, maxNumParent, percentParent, randomPtr); /* * Create a threshold for each of the possible permutation of variable * value instances */ long** thresholdsTable = (long**)SEQ_MALLOC(numVar * sizeof(long*)); assert(thresholdsTable); long v; for (v = 0; v < numVar; v++) { list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long numThreshold = 1 << list_getSize(parentIdListPtr); long* thresholds = (long*)SEQ_MALLOC(numThreshold * sizeof(long)); assert(thresholds); long t; for (t = 0; t < numThreshold; t++) { long threshold = random_generate(randomPtr) % (DATA_PRECISION + 1); thresholds[t] = threshold; } thresholdsTable[v] = thresholds; } /* * Create variable dependency ordering for record generation */ long* order = (long*)SEQ_MALLOC(numVar * sizeof(long)); assert(order); long numOrder = 0; queue_t* workQueuePtr = queue_alloc(-1); assert(workQueuePtr); vector_t* dependencyVectorPtr = vector_alloc(1); assert(dependencyVectorPtr); bitmap_t* orderedBitmapPtr = bitmap_alloc(numVar); assert(orderedBitmapPtr); bitmap_clearAll(orderedBitmapPtr); bitmap_t* doneBitmapPtr = bitmap_alloc(numVar); assert(doneBitmapPtr); bitmap_clearAll(doneBitmapPtr); v = -1; while ((v = bitmap_findClear(doneBitmapPtr, (v + 1))) >= 0) { list_t* childIdListPtr = net_getChildIdListPtr(netPtr, v); long numChild = list_getSize(childIdListPtr); if (numChild == 0) { bool status; /* * Use breadth-first search to find net connected to this leaf */ queue_clear(workQueuePtr); status = queue_push(workQueuePtr, (void*)v); assert(status); while (!queue_isEmpty(workQueuePtr)) { long id = (long)queue_pop(workQueuePtr); status = bitmap_set(doneBitmapPtr, id); assert(status); status = vector_pushBack(dependencyVectorPtr, (void*)id); assert(status); list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id); list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); status = queue_push(workQueuePtr, (void*)parentId); assert(status); } } /* * Create ordering */ long i; long n = vector_getSize(dependencyVectorPtr); for (i = 0; i < n; i++) { long id = (long)vector_popBack(dependencyVectorPtr); if (!bitmap_isSet(orderedBitmapPtr, id)) { bitmap_set(orderedBitmapPtr, id); order[numOrder++] = id; } } } } assert(numOrder == numVar); /* * Create records */ char* record = dataPtr->records; long r; long numRecord = dataPtr->numRecord; for (r = 0; r < numRecord; r++) { long o; for (o = 0; o < numOrder; o++) { long v = order[o]; list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long index = 0; list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); long value = record[parentId]; assert(value != DATA_INIT); index = (index << 1) + value; } long rnd = random_generate(randomPtr) % DATA_PRECISION; long threshold = thresholdsTable[v][index]; record[v] = ((rnd < threshold) ? 1 : 0); } record += numVar; assert(record <= (dataPtr->records + numRecord * numVar)); } /* * Clean up */ bitmap_free(doneBitmapPtr); bitmap_free(orderedBitmapPtr); vector_free(dependencyVectorPtr); queue_free(workQueuePtr); SEQ_FREE(order); for (v = 0; v < numVar; v++) { SEQ_FREE(thresholdsTable[v]); } SEQ_FREE(thresholdsTable); return netPtr; }
int main () { long numNode = 100; puts("Starting tests..."); bool_t status; net_t* netPtr = net_alloc(numNode); assert(netPtr); bitmap_t* visitedBitmapPtr = bitmap_alloc(numNode); assert(visitedBitmapPtr); queue_t* workQueuePtr = queue_alloc(-1); assert(workQueuePtr); assert(!net_isCycle(netPtr)); long aId = 31; long bId = 14; long cId = 5; long dId = 92; net_applyOperation(netPtr, OPERATION_INSERT, aId, bId); assert(net_isPath(netPtr, aId, bId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, bId, aId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, aId, cId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_INSERT, bId, cId); net_applyOperation(netPtr, OPERATION_INSERT, aId, cId); net_applyOperation(netPtr, OPERATION_INSERT, dId, aId); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_INSERT, cId, dId); assert(net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_REVERSE, cId, dId); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_REVERSE, dId, cId); assert(net_isCycle(netPtr)); assert(net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); net_applyOperation(netPtr, OPERATION_REMOVE, cId, dId); assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); bitmap_t* ancestorBitmapPtr = bitmap_alloc(numNode); assert(ancestorBitmapPtr); status = net_findAncestors(netPtr, cId, ancestorBitmapPtr, workQueuePtr); assert(status); assert(bitmap_isSet(ancestorBitmapPtr, aId)); assert(bitmap_isSet(ancestorBitmapPtr, bId)); assert(bitmap_isSet(ancestorBitmapPtr, dId)); assert(bitmap_getNumSet(ancestorBitmapPtr) == 3); bitmap_t* descendantBitmapPtr = bitmap_alloc(numNode); assert(descendantBitmapPtr); status = net_findDescendants(netPtr, aId, descendantBitmapPtr, workQueuePtr); assert(status); assert(bitmap_isSet(descendantBitmapPtr, bId)); assert(bitmap_isSet(descendantBitmapPtr, cId)); assert(bitmap_getNumSet(descendantBitmapPtr) == 2); bitmap_free(visitedBitmapPtr); queue_free(workQueuePtr); bitmap_free(ancestorBitmapPtr); bitmap_free(descendantBitmapPtr); net_free(netPtr); random_t* randomPtr = random_alloc(); assert(randomPtr); netPtr = net_alloc(numNode); assert(netPtr); net_generateRandomEdges(netPtr, 10, 10, randomPtr); net_free(netPtr); puts("All tests passed."); return 0; }
/* * Process interrupts from the WizNet device */ static void w5100_event_s(uint8_t i) { int sn = wiz2sock_map[i]; struct socket *s; uint16_t offset = i << 8; uint16_t stat = w5100_readw(Sn_IR + offset); /* BE read of reg pair */ /* We got a pending event for a dead socket as we killed it. Shoot it again to make sure it's dead */ if (sn == 0xFF) { w5100_writeb(Sn_CR + offset, CLOSE); irqmask &= ~(1 << i); w5100_writeb(IMR, irqmask); return; } s = &sockets[sn]; if (stat & 0x1000) { /* Transmit completed: window re-open. We can allow more data to flow from the user */ s->s_iflag &= ~SI_THROTTLE; wakeup(&s->s_data); } if (stat & 0x800) { /* Timeout */ s->s_error = ETIMEDOUT; w5100_writeb(Sn_CR + offset, CLOSE); w5100_wakeall(s); w5100_eof(s); /* Fall through and let CLOSE state processing do the work */ } if (stat & 0x400) { /* Receive wake: Poke the user in case they are reading */ s->s_iflag |= SI_DATA; wakeup(&s->s_iflag); } if (stat & 0x200) { /* Disconnect: Just kill our host socket. Not clear if this is right or we need to drain data first */ w5100_writeb(Sn_CR + offset, CLOSE); w5100_eof(s); /* When we fall through we'll see CLOSE state and do the actual shutting down */ } if (stat & 0x100) { /* Connect: Move into connected state */ if (s->s_state == SS_CONNECTING) { s->s_state = SS_CONNECTED; wakeup(s); } } /* Clear interrupt sources down */ w5100_writeb(Sn_IR + offset, stat >> 8); switch ((uint8_t)stat) { case 0: /* SOCK_CLOSED */ if (s->s_state != SS_CLOSED && s->s_state != SS_UNUSED) { if (s->s_state != SS_CLOSING && s->s_state != SS_DEAD) { s->s_error = ECONNRESET; /* Sort of a guess */ w5100_wakeall(s); } else wakeup(s); irqmask &= ~(1 << i); w5100_writeb(IMR, irqmask); w5100_eof(s); /* Net layer wants us to burn the socket */ if (s->s_state == SS_DEAD) { wiz2sock_map[i] = 0xFF; sock_closed(s); } else /* so net_close() burns the socket */ s->s_state = SS_CLOSED; } break; case 0x13: /* SOCK_INIT */ break; case 0x14: /* SOCK_LISTEN */ break; case 0x17: /* SOCK_ESTABLISHED */ if (s->s_state == SS_CONNECTING) { s->s_state == SS_CONNECTED; wakeup(s); } else if (s->s_state == SS_LISTENING) { /* * The WizNET believes you allocte a LISTEN socket and * it turns into a connection and you then if need be * allocate another LISTEN socket. * * The socket API believes you set a listening socket * up and it stays listening creating new connected * sockets. * * We do some gymnastics to convince both sides that * what they saw happened. */ int slot; struct socket *ac; int aslot; /* Find a new wiznet slot and socket */ slot = net_alloc(); if (slot == -1 || (ac = sock_alloc_accept(s)) == NULL) { /* No socket free, go back to listen */ w5100_writeb(Sn_CR + offset, CLOSE); net_bind(s); net_listen(s); break; } /* Resources exist so do the juggling */ aslot = ac - sockets; /* Map the existing socket to the new w5100 socket */ sock2wiz_map[sn] = slot; wiz2sock_map[slot] = sn; /* Map the new socket to the existing w5100 socket */ sock2wiz_map[aslot] = i; wiz2sock_map[i] = aslot; /* Now set the new socket back up as it should be */ net_bind(ac); net_listen(ac); /* And kick the accepter */ wakeup(s); } break; case 0x1C: /* SOCK_CLOSE_WAIT */ if (s->s_state == SS_CONNECTED || s->s_state == SS_CONNECTING) s->s_state = SS_CLOSEWAIT; w5100_eof(s); if (s->s_state == SS_ACCEPTWAIT) { /* HUM ??? */ } break; case 0x22: /* SOCK_UDP */ case 0x32: /* SOCK_IPRAW */ case 0x42: /* SOCK_MACRAW */ /* Socket has been created */ s->s_state = SS_UNCONNECTED; wakeup(s); break; } }
void build_fft_network(FFT_NET *fft_net, int n, int window_type) /* modifies:fft_net effects: Constructs the fft network as described in fft.h. Butterfly coefficients, read/write indicies, bit reversed load indicies, and array allocations are computed. */ { int cntr, i, j, s; int stages, bps; int **p, **q, *pp, *qp; SAMPLE two_pi_div_n = TWO_PI / n; /* network definition */ fft_net->n = n; fft_net->bps = bps = n/2; for (i = 0, j = n; j > 1; j >>= 1, i++); fft_net->stages = stages = i; fft_net->direction = FORWARD; fft_net->window_type = window_type; fft_net->next = (FFT_NET *)0; /* allocate registers, index, coefficient arrays */ net_alloc(fft_net); /* create appropriate windows */ if (window_type==HANNING) { create_hanning(fft_net->window, n, 1.); create_hanning(fft_net->inv_window, n, 1./n); } else { create_rectangular(fft_net->window, n, 1.); create_rectangular(fft_net->inv_window, n, 1./n); } /* calculate butterfly coefficients */ { int num_diff_coeffs, power_inc, power; SAMPLE *coeffpr = fft_net->coeffr; SAMPLE *coeffpi = fft_net->coeffi; SAMPLE *inv_coeffpr = fft_net->inv_coeffr; SAMPLE *inv_coeffpi = fft_net->inv_coeffi; /* stage one coeffs are 1 + 0j */ for (i = 0; i < bps; i++) { *coeffpr = *inv_coeffpr = 1.; *coeffpi = *inv_coeffpi = 0.; coeffpr++; inv_coeffpr++; coeffpi++; inv_coeffpi++; } /* stage 2 to last stage coeffs need calculation */ /* (1<<r <=> 2^r */ for (s = 2; s <= stages; s++) { num_diff_coeffs = n / (1 << (stages - s + 1)); power_inc = 1 << (stages -s); cntr = 0; for (i = bps/num_diff_coeffs; i > 0; i--) { power = 0; for (j = num_diff_coeffs; j > 0; j--) { *coeffpr = cos(two_pi_div_n*power); *inv_coeffpr = cos(two_pi_div_n*power); /* AAA change these signs */ *coeffpi = -sin(two_pi_div_n*power); /* change back */ *inv_coeffpi = sin(two_pi_div_n*power); power += power_inc; coeffpr++; inv_coeffpr++; coeffpi++; inv_coeffpi++; } } } } /* calculate network indicies: stage exchange indicies are calculated and then used as offset values from the base register locations. The final addresses are then stored in fft_net. */ { int index, inc; SAMPLE **indexpr = fft_net->indexpr; SAMPLE **indexpi = fft_net->indexpi; SAMPLE **indexqr = fft_net->indexqr; SAMPLE **indexqi = fft_net->indexqi; SAMPLE *regr = fft_net->regr; SAMPLE *regi = fft_net->regi; /* allocate temporary 2d stage exchange index, 1d temp load index */ p = (int **)malloc(stages * PNTR_SIZE); q = (int **)malloc(stages * PNTR_SIZE); for (s = 0; s < stages; s++) { p[s] = (int *)malloc(bps * INT_SIZE); q[s] = (int *)malloc(bps * INT_SIZE); } /* calculate stage exchange indicies: */ for (s = 0; s < stages; s++) { pp = p[s]; qp = q[s]; inc = 1 << s; cntr = 1 << (stages-s-1); i = j = index = 0; do { do { qp[i] = index + inc; pp[i++] = index++; } while (++j < inc); index = qp[i-1] + 1; j = 0; } while (--cntr); } /* compute actual address values using indicies as offsets */ for (s = 0; s < stages; s++) { for (i = 0; i < bps; i++) { *indexpr++ = regr + p[s][i]; *indexpi++ = regi + p[s][i]; *indexqr++ = regr + q[s][i]; *indexqi++ = regi + q[s][i]; } } } /* calculate load indicies (bit reverse ordering) */ /* bit reverse ordering achieved by passing normal order indicies backwards through the network */ /* init to normal order indicies */ { int *load_index,*load_indexp; int *temp_indexp, *temp_index; temp_index=temp_indexp=(int *)malloc(n * INT_SIZE); i = 0; j = n; load_index = load_indexp = fft_net->load_index; while (j--) *load_indexp++ = i++; /* pass indicies backwards through net */ for (s = stages - 1; s > 0; s--) { pp = p[s]; qp = q[s]; for (i = 0; i < bps; i++) { temp_index[pp[i]]=load_index[2*i]; temp_index[qp[i]]=load_index[2*i+1]; } j = n; load_indexp = load_index; temp_indexp = temp_index; while (j--) *load_indexp++ = *temp_indexp++; } /* free all temporary arrays */ free((char *)temp_index); for (s = 0; s < stages; s++) { free((char *)p[s]);free((char *)q[s]); } free((char *)p);free((char *)q); } }