Ejemplo n.º 1
0
int main(int argc, char **argv)
{
        char *archive_name;
        char *buf;
        char *sizebuf;
        int fd;
        struct tar_header *thdr;

        archive_name = (char *)check_malloc(255);
        buf = (char *)check_malloc(NAME_MAX);
        sizebuf= (char *)check_malloc(12);
        archive_name = "file.tar";

        fd = open(archive_name, O_RDONLY);
        if(fd == -1)
        {
                perror("Failed to open file!");
                exit(1);
        }

        /* Read header */
        read(fd, buf, 512);
        thdr = (struct tar_header *)buf; 
        printf("%s\n", thdr->name);
        memcpy(sizebuf, thdr->size, 12);
        printf("%s\n", sizebuf);

        return(0);
}
Ejemplo n.º 2
0
//TODO: Arreglar para mapear el complementario reverso cuando trabajo con
// el análisis con menos de 4 bases. ¿¿¿Esto está hecho???
void reverse_strand_C(vector *r_C, vector *s_C, vector *r_C1, vector *s_C1) {

	r_C->n  = s_C->n; r_C1->n = s_C1->n;

	r_C->vector  = (SA_TYPE *)malloc(r_C->n  * sizeof(SA_TYPE));
	check_malloc(r_C->vector,  "reverseStrandC r_C");
	r_C1->vector = (SA_TYPE *)malloc(r_C1->n * sizeof(SA_TYPE));
	check_malloc(r_C1->vector, "reverseStrandC r_C1");

  if (AA != (uint8_t) -1 && TT != (uint8_t) -1) {
    r_C->vector[AA] = s_C->vector[TT]; r_C1->vector[AA] = s_C1->vector[TT];
    r_C->vector[TT] = s_C->vector[AA]; r_C1->vector[TT] = s_C1->vector[AA];
  } else if (AA != (uint8_t) -1) {
    r_C->vector[AA] = s_C->vector[AA]; r_C1->vector[AA] = s_C1->vector[AA];
  } else if (TT != (uint8_t) -1) {
    r_C->vector[TT] = s_C->vector[TT]; r_C1->vector[TT] = s_C1->vector[TT];
  }

  if (CC != (uint8_t) -1 && GG != (uint8_t) -1) {
    r_C->vector[CC] = s_C->vector[GG]; r_C1->vector[CC] = s_C1->vector[GG];
    r_C->vector[GG] = s_C->vector[CC]; r_C1->vector[GG] = s_C1->vector[CC];
  } else if (CC != (uint8_t) -1) {
    r_C->vector[CC] = s_C->vector[CC]; r_C1->vector[CC] = s_C1->vector[CC];
  } else if (GG != (uint8_t) -1) {
    r_C->vector[GG] = s_C->vector[GG]; r_C1->vector[GG] = s_C1->vector[GG];
  }

}
Ejemplo n.º 3
0
void read_bins(FILE *f, dataset *d) {
	int i;
	
	/* First the number of bins. */
	fscanf(f, "%d", &d->num_bins);

	if (d->num_bins <= 0) {
		fprintf(stderr, "Invalid number of bins: %d\n",
				d->num_bins);
		exit(EXIT_FAILURE);
	}

	/* Allocate space. */
	printf("Allocating space for %d bins...\n", d->num_bins);
	d->bins = (LDOUBLE**)malloc(d->num_bins * sizeof(LDOUBLE*));
	check_malloc(d->bins, __LINE__);

	for (i=0; i < d->num_bins; i++) {
		d->bins[i] = (LDOUBLE*)malloc(2 * sizeof(LDOUBLE));
		check_malloc(d->bins[i], __LINE__); 
	}

	/* Read the bins. */
	for (i=0; i < d->num_bins; i++) {
		printf("Reading in bin %d... ", i);
		fscanf(f, "%lg %lg", &d->bins[i][0], &d->bins[i][1]);
		printf("%lg %lg\n", d->bins[i][0], d->bins[i][1]);
	}
}
Ejemplo n.º 4
0
static void
load_notfunction(const char *filename)
{
	FILE *ip;
	STRBUF *sb = strbuf_open(0);
	STRBUF *ib = strbuf_open(0);
	char *p;
	int i;

	if ((ip = fopen(filename, "r")) == NULL)
		die("'%s' cannot read.", filename);
	for (tablesize = 0; (p = strbuf_fgets(ib, ip, STRBUF_NOCRLF)) != NULL; tablesize++)
		strbuf_puts0(sb, p);
	fclose(ip);
	words = (struct words *)check_malloc(sizeof(struct words) * tablesize);
	/*
	 * Don't free *p.
	 */
	p = (char *)check_malloc(strbuf_getlen(sb) + 1);
	memcpy(p, strbuf_value(sb), strbuf_getlen(sb) + 1);
	for (i = 0; i < tablesize; i++) {
		words[i].name = p;
		p += strlen(p) + 1;
	}
	qsort(words, tablesize, sizeof(struct words), cmp);
	strbuf_close(sb);
	strbuf_close(ib);
}
Ejemplo n.º 5
0
void init_mpi_requests(comm_data *cd, int dim2)
{
  int i;
  const int max_elem_sz = NGRAD * 3;
  ASSERT(dim2 == max_elem_sz);  
  size_t szd = sizeof(double);

  ASSERT(cd->nrecv == 0);
  ASSERT(cd->nsend == 0);
  ASSERT(cd->nreq == 0);
  
  size_t szr = 2 * cd->ncommdomains * sizeof(MPI_Request);
  size_t szs = 2 * cd->ncommdomains * sizeof(MPI_Status);   
  cd->req   = (MPI_Request *)check_malloc(szr);
  cd->stat  = (MPI_Status  *)check_malloc(szs);
  cd->nreq  = cd->ncommdomains;

  int rsz = 0, ssz = 0;
  for(i = 0; i < cd->ncommdomains; i++)
    {
      int k = cd->commpartner[i];
      ssz += dim2 * cd->sendcount[k] * max_elem_sz * szd;
      rsz += dim2 * cd->recvcount[k] * max_elem_sz * szd;
    }  

  cd->sendbuf = check_malloc(ssz);
  cd->nsend = ssz;

  cd->recvbuf = check_malloc(rsz);
  cd->nrecv = rsz;

}
Ejemplo n.º 6
0
void reverse_strand_O(comp_matrix *r_O, comp_matrix *s_O) {

  r_O->siz = s_O->siz;

  r_O->n_desp = s_O->n_desp;
  r_O->m_desp = s_O->m_desp;

	r_O->desp = (SA_TYPE **) malloc(r_O->n_desp * sizeof(SA_TYPE *));
	check_malloc(r_O->desp, "reverse_strand_O");

  if (AA != (uint8_t) -1 && TT != (uint8_t) -1) {
    r_O->desp[AA] = s_O->desp[TT];
    r_O->desp[TT] = s_O->desp[AA];
  } else if (AA != (uint8_t) -1) {
    r_O->desp[AA] = s_O->desp[AA];
  } else if (TT != (uint8_t) -1) {
    r_O->desp[TT] = s_O->desp[TT];
  }

  if (CC != (uint8_t) -1 && GG != (uint8_t) -1) {
    r_O->desp[CC] = s_O->desp[GG];
    r_O->desp[GG] = s_O->desp[CC];
  } else if (CC != (uint8_t) -1) {
    r_O->desp[CC] = s_O->desp[CC];
  } else if (GG != (uint8_t) -1) {
    r_O->desp[GG] = s_O->desp[GG];
  }

#if defined FM_COMP_32 || FM_COMP_64

  r_O->n_count = s_O->n_count;
  r_O->m_count = s_O->m_count;

	r_O->count = (FM_COMP_TYPE **) malloc(r_O->n_count * sizeof(FM_COMP_TYPE *));
	check_malloc(r_O->count, "reverse_strand_O");

  if (AA != (uint8_t) -1 && TT != (uint8_t) -1) {
    r_O->count[AA] = s_O->count[TT];
    r_O->count[TT] = s_O->count[AA];
  } else if (AA != (uint8_t) -1) {
    r_O->count[AA] = s_O->count[AA];
  } else if (TT != (uint8_t) -1) {
    r_O->count[TT] = s_O->count[TT];
  }

if (CC != (uint8_t) -1 && GG != (uint8_t) -1) {
    r_O->count[CC] = s_O->count[GG];
    r_O->count[GG] = s_O->count[CC];
  } else if (CC != (uint8_t) -1) {
    r_O->count[CC] = s_O->count[CC];
  } else if (GG != (uint8_t) -1) {
    r_O->count[GG] = s_O->count[GG];
  }

#endif

}
Ejemplo n.º 7
0
indextype NewEntry(struct matrix *m) {
  /* WARNING: This function modifies the m->keys, m->data and
     m->next. Please do not use the return value of this function directly
     in an assigment. m->next[a]=NewEntry(m) can fail and cause lots of
     trouble.
  */

  if (m->num_entries >= m->datasize) {
    m->datasize *= 2;
    m->keys = (byte *)realloc(m->keys, m->datasize * m->key_size);
    check_malloc(m->keys, "realloc keys");
    m->data = (byte *)realloc(m->data, m->datasize * m->size_of_entry);
    check_malloc(m->data, "realloc data");
    m->next = (indextype *)realloc(m->next, m->datasize * sizeof(indextype));
    check_malloc(m->next, "realloc next pointers");
    m->prev = (indextype *)realloc(m->prev, m->datasize * sizeof(indextype));
    check_malloc(m->prev, "realloc prev pointers");
  } else if (m->allow_shrinking && (m->num_entries < m->datasize / 3 &&
                                    m->num_entries > m->hashsize)) {
    m->datasize /= 2;
    m->keys = (byte *)realloc(m->keys, m->datasize * m->key_size);
    check_malloc(m->keys, "shrink");
    m->data = (byte *)realloc(m->data, m->datasize * m->size_of_entry);
    check_malloc(m->data, "shrink");
    m->next = (indextype *)realloc(m->next, m->datasize * sizeof(indextype));
    check_malloc(m->next, "shrink");
    m->prev = (indextype *)realloc(m->prev, m->datasize * sizeof(indextype));
    check_malloc(m->prev, "shrink");
  }
  return (m->num_entries++);
}
Ejemplo n.º 8
0
Shared *make_shared (int end)
{
  int i;
  Shared *shared = check_malloc (sizeof (Shared));

  shared->counter = 0;
  shared->end = end;

  shared->array = check_malloc (shared->end * sizeof(int));
  for (i=0; i<shared->end; i++) {
    shared->array[i] = 0;
  }
  return shared;
}
void gather_columns(int rank,int num_tasks,float** A){

	float *sendbuf,*recvbuf;
	sendbuf=(float*) check_malloc(parms->num_row * parms->num_my_col * sizeof(float),"Allocation failed for sendbuf in gather_columns");
	if (rank==MASTER){
		recvbuf=(float*) check_malloc(parms->num_row * parms->num_col * sizeof(float),"Allocation failed for recvbuf in gather_columns");
	}
	int sendcount = parms->num_row * parms->num_my_col , *disp, *recvcount;

	//All MPI-tasks fill the send buffer
	for( int j=0; j< parms->num_my_col; j++){
		for (int i=0; i< parms->num_row; i++){
			sendbuf[j*parms->num_row + i] = parms->my_col[i][j];
		}
	}

	if (rank==MASTER){
		recvcount=(int*) check_malloc(num_tasks * sizeof(int),"Allocation failed for recvcount in gather_columns");
		disp=(int*) check_malloc(num_tasks * sizeof(int),"Allocation failed for disp in gather_columns");
		int tmp_disp=0;
		for (int i=0; i < num_tasks; i++){
			disp[i]=tmp_disp;
			if (i < num_tasks -1){
				recvcount[i]=parms->num_row * (parms->col_start_indx[rank+1] - parms->col_start_indx[rank]);
				tmp_disp += parms->num_row * (parms->col_start_indx[rank+1] - parms->col_start_indx[rank]);
			}else if (i == num_tasks-1){
				recvcount[i]=parms->num_row * ( parms->num_col - parms->col_start_indx[rank]);
				tmp_disp += parms->num_row * ( parms->num_col - parms->col_start_indx[rank]);
			}	 	
		}
	}

	check_flag(MPI_Gatherv(sendbuf,sendcount,MPI_FLOAT,recvbuf,recvcount,disp,MPI_FLOAT,MASTER,MPI_COMM_WORLD),7);

	//Now populate the matrix A with the new values gathered from workers

	if (rank == MASTER){
		for (int j=0; j< parms->num_col; j++){
			for (int i=0; i< parms->num_row; i++){
				A[i][j] = recvbuf[j*parms->num_row +i];
			}	
		}
	free(recvbuf);
	free(disp);
	free(recvcount);
	printf("Gathered\n");
	}
}
Ejemplo n.º 10
0
t_arraylist	*ls_gen_filedirs(char *options, t_filedir *temp_filedir)
{
	t_arraylist		*filedirs;
	DIR				*curr_dir;
	t_dirent		*dir_entry;
	char			*root;
	char			*full_path;

	if (!(curr_dir = opendir(temp_filedir->path)))
		return (ls_perror(temp_filedir->name));
	root = ft_strjoin(temp_filedir->path, "/");
	filedirs = NULL;
	while ((dir_entry = readdir(curr_dir)) != NULL)
	{
		full_path = ft_strjoin(root, dir_entry->d_name);
		if (!(temp_filedir = filedir(full_path)))
			continue;
		if (!filedirs)
			filedirs = check_malloc(arraylist(temp_filedir, DIR_BUF));
		else
			filedirs->push(filedirs, temp_filedir);
		free(full_path);
	}
	closedir(curr_dir);
	ls_gen_filedirs_sort(options, filedirs);
	free(root);
	return (filedirs);
}
Ejemplo n.º 11
0
void read_comp_vector(comp_vector *vector, const char *directory, const char *name) {

  size_t err=0;
  FILE *fp;

	char path[500];
  path[0]='\0';
  strcat(path, directory);
  strcat(path, "/");
  strcat(path, name);
  strcat(path, ".vec");

	fp  = fopen(path,  "rb+");
  check_file_open(fp, path);

	err = fread(&vector->siz, sizeof(SA_TYPE),  1, fp);
  check_file_read(err, 1, path);

	err = fread(&vector->n, sizeof(SA_TYPE),  1, fp);
  check_file_read(err, 1, path);

  err = fread(&vector->ratio, sizeof(SA_TYPE),  1, fp);
  check_file_read(err, 1, path);

  vector->vector = (SA_TYPE *) malloc(vector->n * sizeof(SA_TYPE));
  check_malloc(vector->vector, path);

  err = fread(vector->vector, sizeof(SA_TYPE), vector->n, fp);
  check_file_read(err, vector->n, path);

  fclose(fp);
}
Ejemplo n.º 12
0
Semaphore *make_semaphore (int n)
{
  Semaphore *sem = check_malloc (sizeof(Semaphore));
  int ret = sem_init(sem, 0, n);
  if (ret == -1) perror_exit ("sem_init failed");
  return sem;
}
Ejemplo n.º 13
0
Semaphore *make_semaphore(int value)
{
  Semaphore *sem = check_malloc(sizeof(Semaphore));
  int n = sem_init(sem, 0, value);
  if (n != 0) perror_exit("sem_init failed");
  return sem;
}
Ejemplo n.º 14
0
/*
 *  xcalloc() - same as calloc(3).  Used for portability.
 *  Never returns NULL; fatal on error.
 */
void *
xcalloc(size_t n, size_t sz)
{
    void *p;

    if (n < 1)
	n = 1;
    if (sz < 1)
	sz = 1;
    if ((p = calloc(n, sz)) == NULL) {
	if (failure_notify) {
	    snprintf(msg, 128, "xcalloc: Unable to allocate %u blocks of %u bytes!\n",
		(unsigned int) n, (unsigned int) sz);
	    (*failure_notify) (msg);
	} else {
	    perror("xcalloc");
	}
	exit(1);
    }
#if XMALLOC_DEBUG
    check_malloc(p, sz * n);
#endif
#if XMALLOC_STATISTICS
    malloc_stat(sz * n);
#endif
#if XMALLOC_TRACE
    xmalloc_show_trace(p, 1);
#endif
#if MEM_GEN_TRACE
    if (tracefp)
	fprintf(tracefp, "c:%u:%u:%p\n", (unsigned int) n, (unsigned int) sz, p);
#endif
    return (p);
}
Ejemplo n.º 15
0
/*
 *  xmalloc() - same as malloc(3).  Used for portability.
 *  Never returns NULL; fatal on error.
 */
void *
xmalloc(size_t sz)
{
    void *p;

    if (sz < 1)
	sz = 1;

    if ((p = malloc(sz)) == NULL) {
	if (failure_notify) {
	    snprintf(msg, 128, "xmalloc: Unable to allocate %d bytes!\n",
		(int) sz);
	    (*failure_notify) (msg);
	} else {
	    perror("malloc");
	}
	exit(1);
    }
#if XMALLOC_DEBUG
    check_malloc(p, sz);
#endif
#if XMALLOC_STATISTICS
    malloc_stat(sz);
#endif
#if XMALLOC_TRACE
    xmalloc_show_trace(p, 1);
#endif
#if MEM_GEN_TRACE
    if (tracefp)
	fprintf(tracefp, "m:%d:%p\n", sz, p);
#endif
    return (p);
}
Ejemplo n.º 16
0
/**
 * varray_assign: assign varray entry.
 *
 *	@param[in]	vb	#VARRAY structure
 *	@param[in]	index	index
 *	@param[in]	force	if entry not found, create it.
 *	@return		pointer of the entry
 *
 * If specified entry is found then it is returned, else it is allocated
 * and returned.
 * This procedure @EMPH{doesn't} operate the contents of the array.
 */
void *
varray_assign(VARRAY *vb, int index, int force)
{
	if (index < 0)
		die("varray_assign: illegal index value.");
	if (index >= vb->length) {
		if (force)
			vb->length = index + 1;
		else if (index == 0 && vb->length == 0)
			return NULL;
		else
			die("varray_assign: index(=%d) is out of range.", index);
	}
	/*
 	 * Expand the area.
	 */
	if (index >= vb->alloced) {
		int old_alloced = vb->alloced;

		while (index >= vb->alloced)
			vb->alloced += vb->expand;
		/*
		 * Old implementations of realloc() may crash
		 * when a null pointer is passed.
		 * Therefore, we cannot use realloc(NULL, ...).
		 */
		if (vb->vbuf == NULL)
			vb->vbuf = (char *)check_malloc(vb->size * vb->alloced);
		else
			vb->vbuf = (char *)check_realloc(vb->vbuf, vb->size * vb->alloced);
		if (debug)
			fprintf(stderr, "Expanded: from %d to %d.\n", old_alloced, vb->alloced);
	}
	return (void *)(vb->vbuf + vb->size * index);
}
Ejemplo n.º 17
0
void get_rare_value(double *count_matrix, unsigned long long width, double rare_percent, unsigned long long *ret_rare_value, unsigned long long  *ret_rare_width) { 
	size_t y, x;
	unsigned long long rare_width = 0;
	double rare_value = 0;

	// allocate * sort a temporary matrix
	double *sorted_count_matrix = malloc(width * sizeof(double));
	check_malloc(sorted_count_matrix, NULL);

	memcpy(sorted_count_matrix, count_matrix, width * sizeof(double));

	qsort(sorted_count_matrix, width, sizeof(double), double_cmp);

	// get our "rare" counts
	for(y = 0; y < width; y++) {
		double percentage = 0;

		rare_value = sorted_count_matrix[y];
		rare_width = 0;
		for(x = 0; x < width; x++) {
			if(count_matrix[x] <= rare_value) {
				rare_width++;
			}
		}
		percentage = (double)rare_width / (double)width;

		if(percentage >= rare_percent)
			break;
	}

	free(sorted_count_matrix);
	*ret_rare_width = rare_width;
	*ret_rare_value = rare_value;
}
Ejemplo n.º 18
0
vlrlist vlrlist_constructor (void)
{
    vlrlist newlist = (vlrlist) check_malloc (sizeof(vlrlist_struct));
    newlist->list = NULL;
    newlist->size = 0;
    return (newlist);
}
Ejemplo n.º 19
0
void read_vector(vector *vector, const char *directory, const char *name) {

  size_t err=0;
  FILE *fp;

	char path[500]; //TODO: Change to dynamic allocation to avoid buffer overflow
  path[0]='\0';
  strcat(path, directory);
  strcat(path, "/");
  strcat(path, name);
  strcat(path, ".vec");

  fp  = fopen(path,  "rb+");
  check_file_open(fp, path);

  err = fread(&vector->n, sizeof(SA_TYPE),  1, fp);
  check_file_read(err, 1, path);

  vector->vector = (SA_TYPE *) malloc(vector->n * sizeof(SA_TYPE));
  check_malloc(vector->vector, path);

  err = fread(vector->vector, sizeof(SA_TYPE), vector->n, fp);
  check_file_read(err, vector->n, path);

	fclose(fp);
}
Ejemplo n.º 20
0
/*
 * check_strdup: allocate memory and copy string to it.
 *
 *	i)	string	original string
 *	r)		allocated memory
 */
char *
check_strdup(const char *string)
{
	char *p = check_malloc(strlen(string) + 1);
	strcpy(p, string);
	return p;
}
Ejemplo n.º 21
0
void register_crdsys_source( crdsys_source_def *csd )
{
    csd->next = sources;
    sources = (crdsys_source_def *) check_malloc( sizeof( crdsys_source_def ) );
    memcpy( sources, csd, sizeof( crdsys_source_def ) );
    update_id++;
}
Ejemplo n.º 22
0
t_arraylist	*ls_dirs(char *options, t_arraylist *filedirs)
{
	t_arraylist		*dirs;
	t_arlst_iter	*iter;
	int				iter_ret;
	t_filedir		*tmp_fldr;

	iter = arlst_iter(filedirs);
	dirs = NULL;
	iter_ret = 1;
	while (iter_ret > 0)
	{
		tmp_fldr = iter->pip(iter, &iter_ret);
		if (!S_ISDIR(tmp_fldr->stats->st_mode))
		{
			if (!ls_isdir(tmp_fldr->path))
				continue;
			if (ft_strchr(options, 'l'))
				continue;
		}
		if (!dirs)
			dirs = check_malloc(arraylist(tmp_fldr, filedirs->size(filedirs)));
		else
			dirs->push(dirs, tmp_fldr);
	}
	free(iter);
	return (dirs);
}
Ejemplo n.º 23
0
void *joe_malloc(size_t size)
{
	struct mcheck *m = (struct mcheck *)malloc(size+sizeof(struct mcheck)+16384);
	unsigned char *ptr = (unsigned char *)m+sizeof(struct mcheck);
	int x;
	m->state = 0;
	m->size = size;
	m->magic=0x55AA55AA;
	m->next = 0;

	if(!size) {
		printf("0 passed to malloc\n");
		*(int *)0=0;
	}
	for(x=0;x!=16384;++x)
		ptr[x+size]=0xAA;
	if(first) {
		last->next = m;
		last = m;
	} else {
		first = last = m;
	}
	check_malloc();
	/* printf("malloc size=%d addr=%x\n",size,ptr); fflush(stdout); */
	return ptr;
}
Ejemplo n.º 24
0
Mutex *make_mutex ()
{
  Mutex *mutex = check_malloc (sizeof(Mutex));
  int n = pthread_mutex_init (mutex, NULL);
  if (n != 0) perror_exit ("make_lock failed"); 
  return mutex;
}
Ejemplo n.º 25
0
Archivo: io.c Proyecto: mrG7/hpg-libs
void read_ref_vector(ref_vector *vector, const char *directory, const char *name) {

  size_t err=0;
  FILE *fp;

  char path[500];

  path[0]='\0';
  strcat(path, directory);
  strcat(path, "/");
  strcat(path, name);
  strcat(path, ".vec");

  fp  = fopen(path,  "rb+");
  check_file_open(fp, path);

  err = fread(&vector->n, sizeof(uint64_t),  1, fp);
  check_file_read(err, 1, path);

  err = fread(&vector->dollar, sizeof(uint64_t),  1, fp);
  check_file_read(err, 1, path);

  check_file_read(err, 1, path);
  vector->vector = (uint8_t *) malloc((vector->n + 1) * sizeof(uint8_t)); //Valgrind errors on dbwt
  check_malloc(vector->vector, path);

  err = fread(vector->vector, sizeof(uint8_t), vector->n, fp);
  check_file_read(err, vector->n, path);

  vector->vector[vector->n] = 0; //Valgrind errors on dbwt

  fclose(fp);

}
MutexWrapper *make_mutex ()
{
  MutexWrapper *my_mutex = check_malloc (sizeof(MutexWrapper));
  int n = pthread_mutex_init (&(my_mutex->mutex), NULL);
  if (n != 0) perror_exit ("make_lock failed"); 
  return my_mutex;
}
Ejemplo n.º 27
0
void read_data(FILE *f, field *d) {
	ULONG i;

	/* First, get the number of points. */
	fscanf(f, "%lu", &d->num_pts);

	if (d->num_pts <= 0) {
		fprintf(stderr, "Invalid number of data points: %lu\n",
				d->num_pts);
		exit(EXIT_FAILURE);
	}
	
	/* Allocate space. */
	printf("Allocating space for %lu points...\n", d->num_pts);
	d->pts = (p3*)malloc((int)d->num_pts * sizeof(p3));
	check_malloc(d->pts, __LINE__);
	
	/* Low and high bounds. */
	printf("Reading high and low bounds...\n");
	fscanf(f, "%lg %lg %lg", 
		&d->low_bound.x, &d->low_bound.y, &d->low_bound.z);
	fscanf(f, "%lg %lg %lg", 
		&d->high_bound.x, &d->high_bound.y, &d->high_bound.z);
	printf("low: %lg %lg %lg high: %lg %lg %lg\n",
		d->low_bound.x, d->low_bound.y, d->low_bound.z,
		d->high_bound.x, d->high_bound.y, d->high_bound.z);

	/* Then get the data. */
	for (i=0; i < d->num_pts; i++) {
		fscanf(f, "%lg %lg %lg", 
			&d->pts[i].x, &d->pts[i].y, &d->pts[i].z);
	}
}
//Semaphore functions
Semaphore *make_semaphore (int value) {
  Semaphore *semaphore = check_malloc (sizeof(Semaphore));
  semaphore->value = value;
  semaphore->wakeups = 0;
  semaphore->mutex = make_mutex ();
  semaphore->cond = make_cond ();
  return semaphore;
}
// MASTER decided the workload and distributes amongst worker
void distribute_columns(int rank, int num_tasks, float** A){
	int *chunk;
	MPI_Request send_req, recv_req;
	parms->col_start_indx = check_malloc(num_tasks*sizeof(int), "Failed to allocated matrix for chunk size");

	// MASTER decides chunk size and Scatters the correponding count of columns the worker processors
	// The first argument of function "adjust_chunk_size" is neq because we are distributing an augmented matrix
	if (rank == MASTER){ 
		chunk = (int*) check_malloc(num_tasks*sizeof(int), "Failed to allocated matrix for chunk size");
		adjust_chunk_size(parms->num_col, num_tasks, chunk, parms->col_start_indx);
		for (int i =0; i<num_tasks; i++)
			printf("%d ",chunk[i]);
		printf("\n");
	}
	check_flag(MPI_Scatter(chunk,1,MPI_INT,&parms->num_my_col,1, MPI_INT, MASTER, MPI_COMM_WORLD),3);	
	check_flag(MPI_Bcast(parms->col_start_indx,num_tasks,MPI_INT,MASTER,MPI_COMM_WORLD),4);
	//Allocate the placeholder for the columns to recieve by the MASTER
	parms->my_col=  allocate_mat(parms->num_row,parms->num_my_col);
	float recv[parms->num_row*parms->num_my_col];
	float* sendbuf;
	int sendcount[num_tasks],disp[num_tasks];
	if (rank == MASTER){
		int count=parms->num_row*parms->num_col;
		sendbuf = (float*) check_malloc(count*sizeof(float),"Failed to allocated sendbuf in distribute_colums");
		int tmp_disp=0;
		for (int i=0; i< num_tasks; i++){
			sendcount[i] = parms->num_row * chunk[i];
			disp[i] = tmp_disp;
			tmp_disp+=parms->num_row * chunk[i];
		}
		for (int col=0; col<parms->num_col; col++){
			for (int row=0; row<parms->num_row; row++){
				sendbuf[col*parms->num_row + row] = A[row][col];
			}
		}
	}
	check_flag(MPI_Scatterv(sendbuf,sendcount,disp,MPI_FLOAT,recv,parms->num_my_col*parms->num_row,MPI_FLOAT,MASTER,MPI_COMM_WORLD),5);
	for (int j=0; j<parms->num_my_col; j++){
		for (int i=0; i< parms->num_row; i++){
			parms->my_col[i][j] = recv[j*parms->num_row + i];
		}
	}

	if (rank ==MASTER) free(sendbuf);

}
Ejemplo n.º 30
0
/*
 *  xrealloc() - same as realloc(3). Used for portability.
 *  Never returns NULL; fatal on error.
 */
void *
xrealloc(void *s, size_t sz)
{
    void *p;

    PROF_start(xrealloc);
#if XMALLOC_TRACE

    xmalloc_show_trace(s, -1);
#endif

    if (sz < 1)
        sz = 1;

#if XMALLOC_DEBUG

    if (s != NULL)
        check_free(s);

#endif

    if ((p = realloc(s, sz)) == NULL) {
        if (failure_notify) {
            snprintf(msg, 128, "xrealloc: Unable to reallocate %d bytes!\n",
                     (int) sz);
            (*failure_notify) (msg);
        } else {
            perror("realloc");
        }

        exit(1);
    }

#if XMALLOC_DEBUG
    check_malloc(p, sz);

#endif
#if XMALLOC_STATISTICS

    malloc_stat(sz);

#endif
#if XMALLOC_TRACE

    xmalloc_show_trace(p, 1);

#endif
#if MEM_GEN_TRACE

    if (tracefp)		/* new ptr, old ptr, new size */
        fprintf(tracefp, "r:%p:%p:%d\n", p, s, sz);

#endif

    PROF_stop(xrealloc);

    return (p);
}