/* __ompc_init_task_pool_simple:
 * Initializes a task pool, for which tasks may be added and taken.  The task
 * pool will be single-level, with 1 task queue allotted per thread.
 */
omp_task_pool_t * __ompc_create_task_pool_simple(int team_size)
{
  int i;
  omp_task_pool_t *new_pool;
  omp_task_queue_level_t *per_thread;

  new_pool = (omp_task_pool_t *) aligned_malloc(sizeof(omp_task_pool_t),
                                                CACHE_LINE_SIZE);
  Is_True(new_pool != NULL, ("__ompc_create_task_pool: couldn't malloc new_pool"));

  new_pool->team_size = team_size;
  new_pool->num_levels = 1;
  new_pool->num_pending_tasks = 0;
  new_pool->level = aligned_malloc(sizeof(omp_task_queue_level_t),
                                   CACHE_LINE_SIZE);
  pthread_mutex_init(&(new_pool->pool_lock), NULL);
  pthread_cond_init(&(new_pool->pool_cond), NULL);

  Is_True(new_pool->level != NULL,
      ("__ompc_create_task_pool: couldn't malloc level"));

  per_thread = &new_pool->level[PER_THREAD];

  per_thread->num_queues = team_size;
  per_thread->task_queue = aligned_malloc(sizeof(omp_queue_t) * team_size,
                                          CACHE_LINE_SIZE);

  Is_True(per_thread->task_queue != NULL,
      ("__ompc_create_task_pool: couldn't malloc per-thread task queue"));

  for (i = 0; i < team_size; i++)
    __ompc_queue_init(&per_thread->task_queue[i], __omp_task_queue_num_slots);

  return new_pool;
}
Exemplo n.º 2
0
int main(void)
{
	void * p = malloc(103);
	void * q = malloc(1000);
	void * r = malloc(7);

	void * x = aligned_malloc(8, 100);
	void * y = aligned_malloc(32, 1035);
	void * z = aligned_malloc(4, 8);

	printf("Raw malloc pointers, no alignment enforced:\n");
	printf("\t%p, %p, %p\n", p, q, r);
	printf("\tNote: you may see 4-8 byte alignment on host PC\n");
	printf("aligned to 8: %p\n", x);
	printf("aligned to 32: %p\n", y);
	printf("aligned to 4: %p\n", z);

	aligned_free(x), x = NULL;
	aligned_free(y), y = NULL;
	aligned_free(z), z = NULL;

	free(p), p = NULL;
	free(q), q = NULL;
	free(r), r = NULL;

	return 0;
}
Exemplo n.º 3
0
void init_sliders()
{	
  int i;
  bitboard_t x,occ;

  for(i = 0; i < 64; i++)
  {//initializing the square hashtable pointers:
    b_moves[i] = (bitboard_t *)aligned_malloc((size_t)(1 << b_bits[i]) * sizeof(bitboard_t),64);
    r_moves[i] = (bitboard_t *)aligned_malloc((size_t)(1 << r_bits[i]) * sizeof(bitboard_t),64);
    //for each square: 
    //  - get each possible occupancy state according the mask of relevant squares
    //  - get the attack config. against each occupancy state
    //  - hashing by the multiplication method to retrieve index: 
    //    the currently generated occupancy state * magic number >> relevant mask bits count:
    //    (Pradyumna Kannan's reduced array access approach.)
    //  - store the attack configuration at position [index] in the hashtable.
    for(x = 0ULL; x < (1ULL << b_bits[i]); x++)
    { occ = get_blockers(x, b_mask[i]);
      b_moves[i][(occ * b_magics[i]) >> b_shift[i]]= get_bishop_atk(i, occ);
    }
    for(x = 0ULL; x < (1ULL << r_bits[i]); x++)
    { occ = get_blockers(x, r_mask[i]);
      r_moves[i][(occ * r_magics[i]) >> r_shift[i]] = get_rook_atk(i, occ);
    }
  }
}
Exemplo n.º 4
0
int *pqsort(int* input, int num_elements, int num_threads)
{
	if(num_threads*num_threads*CACHE_LINE_SIZE > num_elements){
		num_threads = (int)(sqrt(num_elements/CACHE_LINE_SIZE));
	}
	int power_of=1;
	while(power_of < num_threads){
		power_of *= 2;
	}
	num_threads = power_of;
	if(num_threads == 1){
		qsort(input , num_elements, sizeof(int), compare);
		return input;
	}

	// YOUR CODE GOES HERE
	long i;
	sort_data *sort_data_array = (sort_data*)malloc(num_threads*sizeof(sort_data));
	pthread_t *thread_id = (pthread_t*)malloc(num_threads*sizeof(pthread_t));

	all_pivot_data = (pivot_data*)aligned_malloc(num_threads*sizeof(pivot_data));
	//all_pivot_data = (pivot_data*)malloc(num_threads*sizeof(pivot_data));

	/* MAX_THREADS = 1;
	while( MAX_THREADS < num_threads ){
		MAX_THREADS = MAX_THREADS << 1;
	}
	printf("MAX_THREADS %d\n", MAX_THREADS);*/
	//barr = (barrier_node*)malloc(MAX_THREADS*sizeof(barrier_node));
	//barr = (barrier_node*)aligned_malloc(num_threads*sizeof(barrier_node));

	barr = (barrier_node*)malloc(num_threads*sizeof(barrier_node));
	ps = (prefix_sum_node*)aligned_malloc(num_threads*sizeof(prefix_sum_node));
	mylib_init_prefix_sum(ps, num_threads, all_lengths);

	final_pivots = (int*)malloc((num_threads-1)*sizeof(int));
	partitions = (partition_data*)aligned_malloc(num_threads*sizeof(partition_data));
	for(i=0; i<num_threads; i++){
		partitions[i].id = i;
	}

	mylib_init_barrier (barr, num_threads);
	output = (int*)aligned_malloc(num_elements*sizeof(int));

	// Divide input into num_threads chunks
	divide(input, num_elements, num_threads, sort_data_array);

	// create threads for locally sorting the partitions
	for(i=0; i<num_threads; i++){
		pthread_create(&thread_id[i], NULL, run_threads, sort_data_array+i);
		//printf("i %ld\n", thread_id[i]);
	}

	for(i=0; i<num_threads; i++){
		pthread_join(thread_id[i], NULL);
	}

	return output; //return appropriately
}
Exemplo n.º 5
0
int main(int argc, char **argv) {
    char **endptr;
    int alignment = atoi(argv[1]);
    int *p = (int *)aligned_malloc(100, alignment);
    int *q = (int *)aligned_malloc(128, alignment);
    int *r = (int *)aligned_malloc(128, alignment);
    
    printf("%s: %p\n", argv[1], p);
    aligned_free(p);
    aligned_free(q);
    aligned_free(r);
    return 0;
}
Exemplo n.º 6
0
ssize_t write_blockwise(int fd, void *orig_buf, size_t count)
{
	void *hangover_buf, *hangover_buf_base = NULL;
	void *buf, *buf_base = NULL;
	int r, hangover, solid, bsize, alignment;
	ssize_t ret = -1;

	if ((bsize = sector_size(fd)) < 0)
		return bsize;

	hangover = count % bsize;
	solid = count - hangover;
	alignment = get_alignment(fd);

	if ((long)orig_buf & (alignment - 1)) {
		buf = aligned_malloc(&buf_base, count, alignment);
		if (!buf)
			goto out;
		memcpy(buf, orig_buf, count);
	} else
		buf = orig_buf;

	r = write(fd, buf, solid);
	if (r < 0 || r != solid)
		goto out;

	if (hangover) {
		hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
		if (!hangover_buf)
			goto out;

		r = read(fd, hangover_buf, bsize);
		if (r < 0 || r != bsize)
			goto out;

		r = lseek(fd, -bsize, SEEK_CUR);
		if (r < 0)
			goto out;
		memcpy(hangover_buf, (char*)buf + solid, hangover);

		r = write(fd, hangover_buf, bsize);
		if (r < 0 || r != bsize)
			goto out;
	}
	ret = count;
out:
	free(hangover_buf_base);
	if (buf != orig_buf)
		free(buf_base);
	return ret;
}
Exemplo n.º 7
0
void TimgFilterNoiseMplayer::Tprocess::init(int strength,const TnoiseSettings *Icfg)
{
    cfg=*Icfg;
    noise=(int8_t*)aligned_malloc(MAX_NOISE*sizeof(int8_t));
    int i,j;
    for (i=0,j=0; i<MAX_NOISE; i++,j++) {
        if(cfg.uniform)
            if (cfg.averaged)
                if (cfg.pattern) {
                    noise[i]=int8_t((RAND_N(strength)-strength/2)/6+TimgFilterNoise::patt[j%4]*strength*0.25/3);
                } else {
                    noise[i]=int8_t((RAND_N(strength)-strength/2)/3);
                }
            else if (cfg.pattern) {
                noise[i]=int8_t((RAND_N(strength)-strength/2)/2+TimgFilterNoise::patt[j%4]*strength*0.25);
            } else {
                noise[i]=int8_t(RAND_N(strength)-strength/2);
            }
        else {
            double x1, x2, w, y1;
            do {
                x1=2.0*rand()/(float)RAND_MAX-1.0;
                x2=2.0*rand()/(float)RAND_MAX-1.0;
                w=x1*x1+x2*x2;
            } while (w>=1.0);

            w=sqrt((-2.0*log(w))/w);
            y1=x1*w;
            y1*=strength/sqrt(3.0);
            if (cfg.pattern) {
                y1/=2;
                y1+=TimgFilterNoise::patt[j%4]*strength*0.35;
            }
            if      (y1<-128) {
                y1=-128;
            } else if (y1> 127) {
                y1= 127;
            }
            if (cfg.averaged) {
                y1/=3.0;
            }
            noise[i]=(int8_t)y1;
        }
        if (RAND_N(6)==0) {
            j--;
        }
    }

    for (i=0; i<MAX_RES; i++)
        for (j=0; j<3; j++) {
            prev_shift[i][j]=noise+(rand()&(MAX_SHIFT-1));
        }

    if (nonTempRandShift[0]==-1)
        for (i=0; i<MAX_RES; i++) {
            nonTempRandShift[i]=rand()&(MAX_SHIFT-1);
        }

    shiftptr=0;
}
Exemplo n.º 8
0
int start()
{
	error_ptr = no_error;
	error_len = sizeof(no_error) - 1;

	config_init();
	jparse_init();

	id = (unsigned char *)aligned_malloc(32, SHRT_MAX * 8);

	if (id == NULL) {
		api_log_printf("[AK306] Unable to allocate id buffer\r\n");
		error_ptr = err_unable_to_allocate_id_buffer;
		error_len = sizeof(err_unable_to_allocate_id_buffer) - 1;
		return -1;
	}

	api_db_enum_objects(enum_callback, NULL);

	api_listen_udp(api_tag, host.c_str(), dport.c_str(), data);
	api_listen_udp(api_tag, host.c_str(), uport.c_str(), update);

	now = time(NULL);

	api_log_printf("[AK306] Started\r\n");

	return 0;
}
Exemplo n.º 9
0
/* __ompc_list_add_slots
 *  q: the queue to add slots to
 *  num_slots: number of additional slots to allocate for queue
 *             (not contiguous)
 */
static inline void
__ompc_list_add_slots(omp_queue_t *q, int num_slots)
{
  omp_queue_slot_t *new_slots, *tail, *head;
  unsigned int tail_index, head_index;
  int old_num_slots = q->num_slots;

  tail = q->tail;
  head = q->head;

  new_slots = aligned_malloc( sizeof(omp_queue_slot_t) * num_slots,
                              CACHE_LINE_SIZE);
  Is_True(new_slots != NULL, ("couldn't resize the queue"));

  /* think about if we can avoid this initialization */
  memset(new_slots, 0, num_slots*sizeof(omp_queue_slot_t));

  /* link in the newly allocated slots */
  if (tail->next)
    tail->next->prev = NULL;
  if (head->prev)
    head->prev->next = NULL;
  tail->next  = new_slots;
  new_slots[0].prev = tail;
  head->prev = &new_slots[num_slots-1];
  new_slots[num_slots-1].next = head;

  q->num_slots = old_num_slots +  num_slots;
}
Exemplo n.º 10
0
/**
 * gst_buffer_new_and_alloc:
 * @size: the size of the new buffer's data.
 *
 * Creates a newly allocated buffer with data of the given size.
 * The buffer memory is not cleared. If the requested amount of
 * memory can't be allocated, the program will abort. Use
 * gst_buffer_try_new_and_alloc() if you want to handle this case
 * gracefully or have gotten the size to allocate from an untrusted
 * source such as a media stream.
 * 
 *
 * Note that when @size == 0, the buffer data pointer will be NULL.
 *
 * MT safe.
 * Returns: the new #GstBuffer.
 */
GstBuffer *
gst_buffer_new_and_alloc (guint size)
{
  GstBuffer *newbuf;

  newbuf = gst_buffer_new ();

#ifdef HAVE_POSIX_MEMALIGN
  {
    gpointer memptr = NULL;

    if (G_LIKELY (size)) {
      if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
        /* terminate on error like g_memdup() would */
        g_error ("%s: failed to allocate %u bytes", G_STRLOC, size);
      }
    }
    newbuf->malloc_data = (guint8 *) memptr;
    GST_BUFFER_FREE_FUNC (newbuf) = free;
  }
#else
  newbuf->malloc_data = g_malloc (size);
#endif
  GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
  GST_BUFFER_SIZE (newbuf) = size;

  GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);

  return newbuf;
}
Exemplo n.º 11
0
dsp_linknode * new_dsp(int *ierr)
{
  int i=0;
  *ierr=-1;
  dsp_linknode * sp_l=(dsp_linknode *)aligned_malloc(sizeof(dsp_linknode));
  sp_l->number=DSP_MATRIX;

  sp_l->contents.A=NULL;
  sp_l->contents.IA1=NULL;
  sp_l->contents.IA2=NULL;
  sp_l->contents.PB=NULL;
  sp_l->contents.PE=NULL;
  sp_l->contents.BP1=NULL;
  sp_l->contents.BP2=NULL;

  sp_l->contents.FIDA=NULL_FORMAT;

  for(i=0;i<11;i++)
    sp_l->contents.DESCRA[i]='0';

  for(i=0;i<10;i++)
  sp_l->contents.INFOA[i]='0';

  sp_l->pntr=NULL;
  *ierr=0;
return sp_l;
}
Exemplo n.º 12
0
bool check_constant(int size, uint8_t w) {
    uint64_t * prec = aligned_malloc(64, size * sizeof(uint64_t));
    memset(prec,w,size * sizeof(uint64_t));
    bool answer = check(prec,size);
    free(prec);
    return answer;
}
/* __ompc_init_task_pool_simple_2level:
 * Initializes a task pool, for which tasks may be added and taken.
 */
omp_task_pool_t * __ompc_create_task_pool_simple_2level(int team_size)
{
  int i;
  omp_task_pool_t *new_pool;
  omp_task_queue_level_t *per_thread;
  omp_task_queue_level_t *community;

  new_pool = (omp_task_pool_t *) aligned_malloc(sizeof(omp_task_pool_t),
                                                CACHE_LINE_SIZE);
  Is_True(new_pool != NULL, ("__ompc_create_task_pool: couldn't malloc new_pool"));

  new_pool->team_size = team_size;
  new_pool->num_levels = 2;
  new_pool->num_pending_tasks = 0;
  new_pool->level = aligned_malloc(sizeof(omp_task_queue_level_t)*2,
                                   CACHE_LINE_SIZE);
  pthread_mutex_init(&(new_pool->pool_lock), NULL);
  pthread_cond_init(&(new_pool->pool_cond), NULL);

  Is_True(new_pool->level != NULL,
      ("__ompc_create_task_pool: couldn't malloc level"));

  per_thread = &new_pool->level[PER_THREAD];
  community = &new_pool->level[COMMUNITY];

  per_thread->num_queues = team_size;
  per_thread->task_queue = aligned_malloc(sizeof(omp_queue_t) * team_size,
                                          CACHE_LINE_SIZE);
  community->num_queues = 1;
  community->task_queue = aligned_malloc(sizeof(omp_queue_t), CACHE_LINE_SIZE);

  Is_True(per_thread->task_queue != NULL,
      ("__ompc_create_task_pool: couldn't malloc per-thread task queues"));
  Is_True(community->task_queue != NULL,
      ("__ompc_create_task_pool: couldn't malloc community task queue"));

  for (i = 0; i < team_size; i++)
    __ompc_queue_init(&per_thread->task_queue[i], __omp_task_queue_num_slots);

  /* what's a good size for the community queue, as a function of the local queue
   * sizes and the team size? Just going to make it 2 * local queue size for
   * now.
   */
  __ompc_queue_init(community->task_queue, __omp_task_queue_num_slots*2);

  return new_pool;
}
Exemplo n.º 14
0
void TimgFilterLogoaway::Tplane::renderShapeUwe(const TlogoawaySettings *cfg)
{
    if (!uwetable) {
        uwetable=(double*)aligned_malloc(w*h*sizeof(double));
        uweweightsum=(double*)aligned_malloc(w*h*sizeof(double));
        calcUweWeightTable(w,h,cfg->blur);
    }

    int radius=3*(cfg->vhweight+1);
    const unsigned char *modeparambitmap=parambitmapdata+parambitmapstride;
    for (int y=1; y<h-1; y++,modeparambitmap+=parambitmapstride)
        for (int x=1; x<w-1; x++)
            if(modeparambitmap[x]>SHAPEMAP_NOCHANGE &&
                    modeparambitmap[x]<SHAPEMAP_CONTOUR) {
                int lb=x-radius;
                int rb=x+radius;
                int ub=y-radius;
                int db=y+radius;

                if (lb<0) {
                    lb=0;
                }
                if (rb>w-1) {
                    lb=w-1;
                }

                if (ub<0) {
                    ub=0;
                }
                if (db>h-1) {
                    db=h-1;
                }

                double weightsum=0.0;
                double b=0.0;

                for (int sy=ub; sy<=db; sy++)
                    for (int sx=lb; sx<=rb; sx++)
                        if (parambitmapdata[sy*parambitmapstride+sx]>=SHAPEMAP_CONTOUR) {
                            double weight = uwetable[abs(y-sy)*w+abs(x-sx)];
                            b+=logotempdata[sy*logotempstride+sx]*w;
                            weightsum+=weight;
                        }
                int bi = (int) (b/weightsum);
                logotempdata[y*logotempstride+x]=(unsigned char)bi;
            }
}
Exemplo n.º 15
0
int pnt_init()
{ aligned_free((void *)pnt); 
  pnt = 0;
  pnt = (pawn_entry_t *)aligned_malloc((size_t)PNTSIZE*sizeof(pawn_entry_t), PT_ALIGNMENT);
  if(!pnt) return false;
  aligned_wipe_out((void *)pnt, (size_t)PNTSIZE*sizeof(pawn_entry_t), PT_ALIGNMENT);
  return true;
}
Exemplo n.º 16
0
  /*!
    @brief Allocator based allocation of aligned memory

    Allocate a buffer of bytes aligned on an arbitrary alignment using
    a custom allocator.

    @param alloc  The \c Allocator to use for performing allocation
    @param nbytes Number of bytes to allocate.
    @param align  Alignment boundary to follow

    @return A pointer to an aligned memory block of @c nbytes bytes. If the
            allocation fails, it returns a null pointer. For optimization
            purpose, this pointer is marked as properly aligned by using
            compiler specific attributes.
  **/
  template<class Allocator> BOOST_FORCEINLINE
  typename boost::dispatch::meta
                ::enable_if_type<typename Allocator::pointer, void*>::type
  allocate( Allocator& alloc, std::size_t nbytes, std::size_t align )
  {
    return aligned_malloc ( nbytes, align
                          , details::allocator_malloc<Allocator>(alloc)
                          );
  }
Exemplo n.º 17
0
bool check_step(int size, int step) {
    uint64_t * prec = aligned_malloc(64, size * sizeof(uint64_t));
    memset(prec,0,size * sizeof(uint64_t));
    for(int i = 0; i < size * (int) sizeof(uint64_t); i+= step) {
        prec[i/64] |= (UINT64_C(1)<<(i%64));
    }
    bool answer = check(prec,size);
    free(prec);
    return answer;
}
Exemplo n.º 18
0
bool check_continuous(int size, int runstart, int runend) {
    uint64_t * prec = aligned_malloc(64, size * sizeof(uint64_t));
    memset(prec,0,size * sizeof(uint64_t));
    for(int i = runstart; i < runend; ++i) {
        prec[i/64] |= (UINT64_C(1)<<(i%64));
    }
    bool answer = check(prec,size);
    free(prec);
    return answer;
}
Exemplo n.º 19
0
void initialize_inventory(Inventory* inv, int cap) {
    inv->locked = SDL_CreateMutex();
    inv->capacity = cap;
    size_t items_size = sizeof(ItemCommon) * cap;
    inv->items = aligned_malloc(items_size);
    SDL_memset(inv->items, 0, items_size);
    for (int i = 0; i < cap; i++) {
        inv->items[i].item_type_id = ITEM_NONE;
    }
}
Exemplo n.º 20
0
Arquivo: prng.cpp Projeto: sga001/vex
void Prng::init_chacha_png(uint8_t *key, uint8_t *iv) {
  chacha = (ECRYPT_ctx*)aligned_malloc(sizeof(ECRYPT_ctx));
  random_state = new u8[RANDOM_STATE_SIZE];

  ECRYPT_keysetup(chacha, key, 256, 64);
  ECRYPT_ivsetup(chacha, iv);

  chacha_refill_randomness();

  return;
}
Exemplo n.º 21
0
/* Create random image for testing purposes */
static pixman_image_t *
create_random_image (pixman_format_code_t *allowed_formats,
		     int                   max_width,
		     int                   max_height,
		     int                   max_extra_stride,
		     pixman_format_code_t *used_fmt)
{
    int n = 0, i, width, height, stride;
    pixman_format_code_t fmt;
    uint32_t *buf;
    pixman_image_t *img;

    while (allowed_formats[n] != PIXMAN_null)
	n++;

    if (n > N_MOST_LIKELY_FORMATS && lcg_rand_n (4) != 0)
	n = N_MOST_LIKELY_FORMATS;
    fmt = allowed_formats[lcg_rand_n (n)];

    width = lcg_rand_n (max_width) + 1;
    height = lcg_rand_n (max_height) + 1;
    stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 +
	lcg_rand_n (max_extra_stride + 1);
    stride = (stride + 3) & ~3;

    /* do the allocation */
    buf = aligned_malloc (64, stride * height);

    /* initialize image with random data */
    for (i = 0; i < stride * height; i++)
    {
	/* generation is biased to having more 0 or 255 bytes as
	 * they are more likely to be special-cased in code
	 */
	*((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) :
	    (lcg_rand_n (2) ? 0 : 255);
    }

    img = pixman_image_create_bits (fmt, width, height, buf, stride);

    if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
    {
	pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)]));
    }
    else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
    {
	pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)]));
    }

    image_endian_swap (img);

    if (used_fmt) *used_fmt = fmt;
    return img;
}
Exemplo n.º 22
0
void convert_into_pivot_data(int num_threads, int thread_id, int my_length){
	int* myptr = output + ps[thread_id].sum;
	pivot_data* pd = (pivot_data*)aligned_malloc(num_threads * sizeof(pivot_data)); 
	int i;
	for(i=0; i<num_threads; i++){
		pd[i].length = partitions[i].length[thread_id];
		pd[i].pivots = partitions[i].data[thread_id];
		pd[i].ctr = 0;
	}
	merge(num_threads, my_length, pd, myptr);
}
Exemplo n.º 23
0
int main(int argc, const char **argv) {

    while(true) {
        switch (rpc_call) {
        case Message::None:
            break;
        case Message::Alloc:
            set_rpc_return(reinterpret_cast<int>(aligned_malloc(hvx_alignment, RPC_ARG(0))));
            break;
        case Message::Free:
            aligned_free(reinterpret_cast<void*>(RPC_ARG(0)));
            set_rpc_return(0);
            break;
        case Message::InitKernels:
            set_rpc_return(initialize_kernels(
                reinterpret_cast<unsigned char*>(RPC_ARG(0)),
                RPC_ARG(1),
                RPC_ARG(2),
                reinterpret_cast<handle_t*>(RPC_ARG(3))));
            break;
        case Message::GetSymbol:
            set_rpc_return(get_symbol(
                static_cast<handle_t>(RPC_ARG(0)),
                reinterpret_cast<const char *>(RPC_ARG(1)),
                RPC_ARG(2),
                RPC_ARG(3)));
            break;
        case Message::Run:
            set_rpc_return(run(
                static_cast<handle_t>(RPC_ARG(0)),
                static_cast<handle_t>(RPC_ARG(1)),
                reinterpret_cast<const buffer*>(RPC_ARG(2)),
                RPC_ARG(3),
                reinterpret_cast<buffer*>(RPC_ARG(4)),
                RPC_ARG(5),
                reinterpret_cast<const buffer*>(RPC_ARG(6)),
                RPC_ARG(7)));
            break;
        case Message::ReleaseKernels:
            set_rpc_return(release_kernels(
                static_cast<handle_t>(RPC_ARG(0)),
                RPC_ARG(1)));
            break;
        case Message::Break:
            return 0;
        default:
            log_printf("Unknown message: %d\n", rpc_call);
            return -1;
        }
    }
    log_printf("Unreachable!\n");
    return 0;
}
Exemplo n.º 24
0
main()
{
	/* malloc adds a header before the pointer it returns to use in free
	   This header must not be modified and the original pointer must be
	   retrievable. So let's take a cue from malloc's book and add a
	   header of our own. This header is only 1 sizeof(void*) long and will
	   store the original address for use in aligned_free. It will be
	   located 1 sizeof(void*) before the pointer we return. This should
	   operate correctly regardless of the width of the memory address. */
	test = aligned_malloc(malloc_size, malloc_alignment);
	aligned_free(test);
}
Exemplo n.º 25
0
    inline aligned_stack_buffer<T, A>::aligned_stack_buffer(size_type n)
        : m_size(n)
    {
#ifdef XSIMD_ALLOCA
        if (sizeof(T) * n <= XSIMD_STACK_ALLOCATION_LIMIT)
        {
            m_ptr = reinterpret_cast<pointer>(
                (reinterpret_cast<size_t>(XSIMD_ALLOCA(n + A)) &
                 ~(size_t(A - 1))) +
                A);
            m_heap_allocation = false;
        }
        else
        {
            m_ptr = reinterpret_cast<pointer>(aligned_malloc(n, A));
            m_heap_allocation = true;
        }
#else
        m_ptr = reinterpret_cast<pointer>(aligned_malloc(n, A));
        m_heap_allocation = true;
#endif
    }
Exemplo n.º 26
0
int main() 
{
	int bytes_required , alignment ;
	printf("\nEnter the bytes_required and alignment :");
	
	scanf("%d %d",&bytes_required , &alignment);
	
	int *ptr = aligned_malloc(bytes_required,alignment);
	printf("\nAddress: %d\n",ptr);


	return 0;
}
Exemplo n.º 27
0
void partition(sort_data* sort_this){
	int num_threads = sort_this->num_threads;
	int** data = (int**)aligned_malloc(num_threads*sizeof(int*));
	int* length = (int*)aligned_malloc(num_threads*sizeof(int));

	int i, search;
	int elements = sort_this->length;

	data[0] = sort_this->sortMe;
	for(i=1; i<num_threads; i++){
		//printf("thread# %d elements %d final_pivots %d\n", sort_this->thread_id, elements, final_pivots[i-1]);
		length[i-1] = binary(elements, data[i-1], final_pivots[i-1]);
		data[i] = data[i-1] + length[i-1];
		elements -= length[i-1];
		//printf("thread# %d i-1 %d, length %d\n", sort_this->thread_id, i-1, length[i-1]);
	}
	// TODO: free final_pivots double free detected error?? 
	//free(final_pivots);
	length[num_threads-1] = elements;
	//printf("thread# %d i-1 %d, length %d\n", sort_this->thread_id, num_threads-1, length[num_threads-1]);
	partitions[sort_this->thread_id].data = data;
	partitions[sort_this->thread_id].length = length;
}
Exemplo n.º 28
0
void __ompc_queue_lockless_init(omp_queue_t * q, int num_slots)
{
  q->slots = aligned_malloc(
      num_slots * sizeof(omp_queue_slot_t), CACHE_LINE_SIZE);
  Is_True(q->slots != NULL,
      ("__ompc_queue_init: couldn't malloc slots for queue"));
  memset(q->slots, 0, num_slots*sizeof(omp_queue_slot_t));
  q->head = q->tail = q->slots;
  q->num_slots = num_slots;
  q->is_empty = 1;
  q->head_index = q->tail_index = q->used_slots = q->reject =  0;
  __ompc_init_lock(&q->lock1);
  __ompc_init_lock(&q->lock2);
}
Exemplo n.º 29
0
/*
 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
 * but we also need a function to deal with short writes at the start. But this information
 * is implicitly included in the read/write offset, which can not be set to non-aligned
 * boundaries. Hence, we combine llseek with write.
 */
ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset) {
	char *frontPadBuf;
	void *frontPadBuf_base = NULL;
	int r, bsize, frontHang;
	size_t innerCount = 0;
	ssize_t ret = -1;

	if ((bsize = sector_size(fd)) < 0)
		return bsize;

	frontHang = offset % bsize;

	if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
		goto out;

	if (frontHang) {
		frontPadBuf = aligned_malloc(&frontPadBuf_base,
					     bsize, get_alignment(fd));
		if (!frontPadBuf)
			goto out;

		r = read(fd, frontPadBuf, bsize);
		if (r < 0 || r != bsize)
			goto out;

		innerCount = bsize - frontHang;
		if (innerCount > count)
			innerCount = count;

		memcpy(frontPadBuf + frontHang, buf, innerCount);

		if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
			goto out;

		r = write(fd, frontPadBuf, bsize);
		if (r < 0 || r != bsize)
			goto out;

		buf += innerCount;
		count -= innerCount;
	}

	ret = count ? write_blockwise(fd, buf, count) : 0;
	if (ret >= 0)
		ret += innerCount;
out:
	free(frontPadBuf_base);

	return ret;
}
		// NOTE: Caller must free *image with aligned_malloc() from above.
		void FloatArrayToByteArrayWithPadding(const Image<RGBfColor> &float_image,
			unsigned char **image,
			int *image_stride) {
			// Allocate enough so that accessing 16 elements past the end is fine.
			*image_stride = float_image.Width() + 16;
			*image = static_cast<unsigned char *>(
				aligned_malloc(*image_stride * float_image.Height(), 16));
			for (int i = 0; i < float_image.Height(); ++i) {
				for (int j = 0; j < float_image.Width(); ++j) {
					(*image)[*image_stride * i + j] =
						static_cast<unsigned char>(255.0 * float_image(i, j)(0));
				}
			}
		}