Exemplo n.º 1
0
WORD    xtermres(long blkln, WORD rc)
{
    xsetblk(0,run,blkln);

    reserve_block(run, find_mpb(run));

    xterm(rc);
}
Exemplo n.º 2
0
// Tensors must be sorted from most to least significant dimension.
// nc is the number of contracted indices, and
// ind is an array of (nx by 2) integers (pairs of contracted indices).
Tensor *tensdot(Tensor *a, Tensor *b, int nc, int *ind, int nref, Slice *m) {
    int i = 0;
    int ord_a, ord_b;
    int *anum, *bnum;
    int an, bn;
    Tensor *t;

#ifdef PARANOID
    if(nc > a->n || nc > b->n) {
        fprintf(stderr, "tensdor: more contracted than input dims.\n");
        exit(1);
    }
#endif
    anum = number_indices(a->n, nc, ind);
    bnum = number_indices(b->n, nc, ind+1);

    t = tensor_ctor(a->n + b->n - 2*nc);
    an = copy_shape(a, anum, t->shape);
    bn = copy_shape(b, bnum, t->shape + a->n - nc);
    t->len = an*bn;
    t->b = reserve_block(m, nref, t->len*sizeof(float));

    if(nc == 0) { // trivial case A <- a X Y^T + A, A m x n
        GER(bn, an, 1.0, b->b->x, 1, a->b->x, 1,
                t->b->x, an);
        //GER(bn, an, a->scale*b->scale, b->b->x, 1, a->b->x, 1,
        //        t->b->x, an);
        free(anum); free(bnum);
        return t;
    }

    if( (ord_a = ck_ordered(a->n, anum))) {
        if( (ord_b = ck_ordered(b->n, bnum))
                && ck_same(nc, ind)) { // Straightforward dgemm.
            GEMM(ord_b == 1, ord_a == -1, bn, an, a->len / an,
                    1.0, a->b->x, an, b->b->x, bn,
                    0.0, t->b->x, bn);
            //GEMM(ord_b == 1, ord_a == -1, bn, an, a->len / an,
            //      a->scale*b->scale, a->b->x, an, b->b->x, bn,
            //      0.0, t->b->x, bn);
        // TODO: decide if transposing A would give better perf.
        } else { // Need to transpose B.
            printf("Need to transpose B.\n");
        }
    } else if( (ord_b = ck_ordered(b->n, bnum))) { // Need to transpose A
            printf("Need to transpose A.\n");
    } else { // Need to transpose both A and B.
            printf("Need to transpose A & B.\n");
    }

    free(anum);
    free(bnum);
    return t;
}
 int StackAllocator::reserve(const int64_t size)
 {
   int err = OB_SUCCESS;
   if (head_->remain() >= size || reserved_->remain() >= size)
   {}
   else if (OB_SUCCESS != (err = reserve_block(size)))
   {
     TBSYS_LOG(ERROR, "reserve_block(size=%ld)=>%d", size, err);
   }
   return err;
 }
Exemplo n.º 4
0
//****** realloc_fs_blocks*****************
//Allocates at least "blocks_needed" blocks for this file
int8_t realloc_fs_blocks(inode* inode, uint32_t blocks_needed)
{
	uint32_t* s_ind = calloc(1, sizeof(block));
	if(blocks_needed > inode->blocks)
	{
		if(inode->blocks <= 8)
		{
			for(; inode->blocks < MIN(8,blocks_needed); inode->blocks++)
			{
				inode->data0[inode->blocks] = reserve_block();
			}
		}

		if(inode->blocks < blocks_needed) //still not enough after allocating direct
		{
			//Determine if a single indirect block has been allocated
			if(inode->data1 == 0)
			{
				inode->data1 = reserve_block();
			}
			else
			{
				blk_read(inode->data1,(block*)s_ind);
			}
			//Allocate the blocks and save in the indirect block
			for(;inode->blocks < blocks_needed; inode->blocks++)
			{
				s_ind[inode->blocks-8] = reserve_block();
			}
			blk_write(inode->data1,(block*)s_ind);
		}

	}
	free(s_ind);
	return 0;
}
Exemplo n.º 5
0
// Allocate a new tensor.
// x will be managed by MemSpace.
Tensor *mkTensor(const int nd, const int *shape,
                 const int nref, MemSpace *mem) {
    Tensor *t = tensor_ctor(nd, shape);
    t->x = (double *)reserve_block(mem, sizeof(double)*t->len, nref);
    return t;
}
Exemplo n.º 6
0
//******** mkdir ********************
int8_t cnmkdir(const char* name)
{
	char* name_copy = strdup(name);
	char name_tok[256];
	char entry_name[256];
	char* next_name_tok;
	dir_entry* entry;
	dir_ptr *dir = calloc(1,sizeof(dir_ptr));		//Directory file in memory (e.g. DIR object from filedef.h)
	bool last_dir = false;

	inode_read(cwd->inode_id, &dir->inode_st);		//Start at cwd
	dir->inode_id = cwd->inode_id;

	next_name_tok = strtok(name_copy, "/");
	do
	{
		//name_tok is the dir we are searching for or going to create
		strcpy(name_tok, next_name_tok);

		//Read the directory file for this inode
		dir->data = calloc(dir->inode_st.blocks,sizeof(block));  	//Memory for all directory file blocks
		llread(&dir->inode_st, dir->data);	//Read the directory file
		dir->index = 0;

		next_name_tok = strtok(NULL, "/");		//Read the next token
		if(next_name_tok == NULL)   //This is the last directory in the path
		{
			last_dir = true;
		}

		//Find the token in this dir
		while((entry = cnreaddir(dir)))
		{
			memcpy(entry_name, entry->name, entry->name_len);
			entry_name[entry->name_len] = 0;
			if(strcmp(entry_name, name_tok) == 0)  //If this directory already exists
			{
				if(last_dir)
				{
					return -1;   //Directory already exists
				}
				else   //Read the directory inode
				{
					dir->inode_id = entry->inode;
					inode_read(entry->inode,&dir->inode_st);   //Read the next directory's inode
					free(dir->data);  //Forget the directory we just read
					break;
				}
			}
		}

		if(last_dir)  //Create the directory at the end of the list
		{
			//Create parent directory entry
			entry = (dir_entry*)(((uint8_t*)dir->data)+dir->index);
			entry->file_type = ITYPE_DIR;
			entry->inode = reserve_inode();
			memcpy(entry->name,name_tok,strlen(name_tok));
			entry->name_len = strlen(name_tok);
			entry->entry_len = entry->name_len + 8;
			entry->entry_len += (4 - entry->entry_len % 4);  //padding out to 32 bits
			dir->inode_st.size += entry->entry_len;
			//TODO: handle mkdir block overflow

			//Write parent dir and inode
			dir->inode_st.modified = time(NULL);
			inode_write(dir->inode_id, &dir->inode_st);
			blk_write(dir->inode_st.data0[0], dir->data);

			//Write new directory inode
			inode new_dir_i;
			memset(&new_dir_i, 0, sizeof(inode));
			uint32_t now = time(NULL);
			new_dir_i.modified = now;
			new_dir_i.type = ITYPE_DIR;
			new_dir_i.size = 0;
			new_dir_i.blocks = 1;
			new_dir_i.data0[0] = reserve_block();

			//Write new directory file
			block* new_dir_block = calloc(1,sizeof(block));

			// . (self entry)
			dir_entry* new_dir_self_entry = (dir_entry*)new_dir_block;
			new_dir_self_entry->inode = entry->inode;
			new_dir_self_entry->file_type = ITYPE_DIR;
			new_dir_self_entry->name_len = 1;
			new_dir_self_entry->entry_len = 12;
			memcpy(new_dir_self_entry->name, ".", 1);
			new_dir_i.size += 12;

			// .. (parent entry)
			dir_entry* new_dir_parent_entry = (dir_entry*)(((uint8_t*)new_dir_block) + 12);
			new_dir_parent_entry->inode = dir->inode_id;
			new_dir_parent_entry->file_type = ITYPE_DIR;
			new_dir_parent_entry->name_len = 2;
			new_dir_parent_entry->entry_len = 12;
			memcpy(new_dir_parent_entry->name, "..", 2);
			new_dir_i.size += 12;

			//Write new dir and inode
			inode_write(entry->inode, &new_dir_i);
			blk_write(new_dir_i.data0[0], new_dir_block);

			free(new_dir_block);
			break;
		}

	} while(1);
	free(dir);
	free(name_copy);
	return 0;
}