Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoYW007 authored Nov 4, 2022
1 parent f4b9120 commit 7667099
Show file tree
Hide file tree
Showing 30 changed files with 5,806 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
CC := g++
DPU_DIR := dpu
HOST_DIR := host
COMMON_LIB_DIR := include/common
DPU_LIB_DIR := include/dpu
HOST_LIB_DIR := include/host
BUILDDIR ?= build
NR_TASKLETS ?= 16
NR_DPUS ?= 2560

define conf_filename
${BUILDDIR}/.NR_DPUS_$(1)_NR_TASKLETS_$(2).conf
endef
CONF := $(call conf_filename,${NR_DPUS},${NR_TASKLETS})

HOST_TARGET := ${BUILDDIR}/pim_base_host
DPU_TARGET := ${BUILDDIR}/pim_base_dpu

COMMON_DIR := common
COMMON_INCLUDES := $(wildcard ${COMMON_DIR}/*.hpp)
HOST_LIBS := $(wildcard ${HOST_LIB_DIR}/*.hpp)
HOST_INCLUDES := $(wildcard ${HOST_DIR}/*.hpp)
HOST_SOURCES := $(wildcard ${HOST_DIR}/*.cpp)
DPU_LIBS := $(wildcard ${DPU_LIB_DIR}/*.h)
DPU_INCLUDES := $(wildcard ${DPU_DIR}/*.h)
DPU_SOURCES := $(wildcard ${DPU_DIR}/*.c)

.PHONY: all clean test

__dirs := $(shell mkdir -p ${BUILDDIR})

COMMON_FLAGS := -Wall -Wno-unused-function -Wextra -g -I${COMMON_DIR} -I${COMMON_LIB_DIR}
HOST_LIB_FLAGS := -I${HOST_DIR} -isystem parlaylib/include -isystem argparse/include -Itimer_tree/include
HOST_FLAGS := ${COMMON_FLAGS} -std=c++17 -lpthread -O3 ${HOST_LIB_FLAGS} -I${HOST_LIB_DIR} `dpu-pkg-config --cflags --libs dpu` -DNR_TASKLETS=${NR_TASKLETS} -DNR_DPUS=${NR_DPUS}
DPU_FLAGS := ${COMMON_FLAGS} -I${DPU_DIR} -I${DPU_LIB_DIR} -O2 -DNR_TASKLETS=${NR_TASKLETS}

all: ${HOST_TARGET} ${DPU_TARGET}

${CONF}:
$(RM) $(call conf_filename,*,*)
touch ${CONF}

${HOST_TARGET}: ${HOST_SOURCES} ${HOST_LIBS} ${HOST_INCLUDES} ${COMMON_INCLUDES} ${CONF}
$(CC) -o $@ ${HOST_SOURCES} ${HOST_FLAGS}

${DPU_TARGET}: ${DPU_SOURCES} ${DPU_LIBS} ${COMMON_INCLUDES} ${CONF}
dpu-upmem-dpurte-clang ${DPU_FLAGS} -o $@ ${DPU_SOURCES}

clean:
$(RM) -r $(BUILDDIR)

test_c: ${HOST_TARGET} ${DPU_TARGET}
./${HOST_TARGET}

test: test_c

6 changes: 6 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define XSTR(x) STR(x)
#define STR(x) #x

#include <stdint.h>
45 changes: 45 additions & 0 deletions common/task_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include <stdint.h>
#include "pptr.h"

// name
// id
// fixed : true for fixed
// length (expected)
// content
#ifndef TASK
#define TASK(NAME, ID, FIXED, LENGTH, CONTENT)
#endif

TASK(empty_task, 0, true, 0, {})

TASK(fixed_task, 1, true, sizeof(fixed_task), {
pptr addr;
int64_t a[1];
})

TASK(fixed_reply, 2, true, sizeof(fixed_reply), { int64_t a[1]; })

// #define FIXED_TSK 1
// typedef struct {
// pptr addr;
// int64_t a[3];
// } fixed_task;

// #define FIXED_REP 2
// typedef struct {
// int64_t a[1];
// } fixed_reply;

// #define VARLEN_TSK 3
// typedef struct {
// pptr addr;
// int64_t len;
// int64_t val[];
// } varlen_task;

// #define VARLEN_REP 4
// typedef struct {
// int64_t len;
// int64_t val[];
// } varlen_reply;
94 changes: 94 additions & 0 deletions dpu/dpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2014-2017 - uPmem
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* An example of checksum computation with multiple tasklets.
*
* Every tasklet processes specific areas of the MRAM, following the "rake"
* strategy:
* - Tasklet number T is first processing block number TxN, where N is a
* constant block size
* - It then handles block number (TxN) + (NxM) where M is the number of
* scheduled tasklets
* - And so on...
*
* The host is in charge of computing the final checksum by adding all the
* individual results.
*/
#include <defs.h>
#include <mram.h>
#include <alloc.h>
#include <perfcounter.h>
#include <barrier.h>
#include <stdint.h>
#include <stdio.h>
#include "driver.h"
#include "task.h"
#include "task_framework_dpu.h"


void exec_fixed_task(int lft, int rt) {
init_block_with_type(fixed_task, fixed_reply);

init_task_reader(lft);
// printf("%d\t%d\t%d\t%d\t%d\t%d\n", task_len(fixed_task), fixed_task_len, sizeof(fixed_task), recv_block_fixlen, tid, lft, get_task_cached(lft));
for (int i = lft; i < rt; i ++) {
fixed_task* ft = (fixed_task*)get_task_cached(i);
IN_DPU_ASSERT(ft->addr.id < 10000, "???");
if (i == lft) {
printf("fixed: id=%llx\nl=%d\tr=%d\n", PPTR_TO_I64(ft->addr), lft,
rt);
}
fixed_reply fr;
fr.a[0] = PPTR_TO_I64(ft->addr);
push_fixed_reply(i, &fr);
}
}

void exec_varlen_task(int lft, int rt) {
(void)lft;
(void)rt;
}

void execute(int lft, int rt) {
uint32_t tid = me();
switch (recv_block_task_type) {
case fixed_task_id: {
exec_fixed_task(lft, rt);
break;
}
// case : {
// exec_varlen_task(lft, rt);
// break;
// }
default: {
printf("TT = %lld\n", recv_block_task_type);
IN_DPU_ASSERT(false, "WTT\n");
break;
}
}
// EXIT();
finish_reply(recv_block_task_cnt, tid);
}

void init() {

}

int main() {
run();
return 0;
}
62 changes: 62 additions & 0 deletions dpu/storage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include <mram.h>

#include "macro.h"

__host mpuint8_t wram_heap_save_addr = NULL_pt(mpuint8_t);

__mram_noinit int64_t
send_varlen_offset_tmp[NR_TASKLETS][MAX_TASK_COUNT_PER_TASKLET_PER_BLOCK];
__mram_noinit uint8_t
send_varlen_buffer_tmp[NR_TASKLETS][MAX_TASK_BUFFER_SIZE_PER_TASKLET];

// dpu.c
int64_t DPU_ID; // = -1;

// task_dpu.h
extern mpint64_t send_varlen_offset[];
extern mpuint8_t send_varlen_buffer[];

typedef struct WRAMHeap {
int64_t DPU_ID;
mpint64_t send_varlen_offset[NR_TASKLETS];
mpuint8_t send_varlen_buffer[NR_TASKLETS];
} WRAMHeap; //` __attribute__((aligned (8)));

__mram_noinit uint8_t wram_heap_save_addr_tmp[sizeof(WRAMHeap) << 1];

void wram_heap_save() {
mpuint8_t saveAddr = wram_heap_save_addr;
WRAMHeap heapInfo = (WRAMHeap){.DPU_ID = DPU_ID};
for (int i = 0; i < NR_TASKLETS; i++) {
heapInfo.send_varlen_offset[i] = send_varlen_offset[i];
heapInfo.send_varlen_buffer[i] = send_varlen_buffer[i];
}

if (saveAddr == NULL_pt(mpuint8_t)) saveAddr = wram_heap_save_addr_tmp;
mram_write(&heapInfo, (mpuint8_t)saveAddr, sizeof(WRAMHeap));
wram_heap_save_addr = saveAddr;
}

void wram_heap_init() {
for (int i = 0; i < NR_TASKLETS; i++) {
send_varlen_offset[i] = &(send_varlen_offset_tmp[i][0]);
send_varlen_buffer[i] = &(send_varlen_buffer_tmp[i][0]);
}
}

void wram_heap_load() {
mpuint8_t saveAddr = wram_heap_save_addr;
if (saveAddr == NULL_pt(mpuint8_t))
wram_heap_init();
else {
WRAMHeap heapInfo;
mram_read((mpuint8_t)saveAddr, &heapInfo, sizeof(WRAMHeap));
DPU_ID = heapInfo.DPU_ID;
for (int i = 0; i < NR_TASKLETS; i++) {
send_varlen_offset[i] = heapInfo.send_varlen_offset[i];
send_varlen_buffer[i] = heapInfo.send_varlen_buffer[i];
}
}
}
68 changes: 68 additions & 0 deletions host/barrier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifdef __APPLE__

#ifndef PTHREAD_BARRIER_H_
#define PTHREAD_BARRIER_H_

#include <pthread.h>
#include <errno.h>

typedef int pthread_barrierattr_t;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
int tripCount;
} pthread_barrier_t;


int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
{
if(count == 0)
{
errno = EINVAL;
return -1;
}
if(pthread_mutex_init(&barrier->mutex, 0) < 0)
{
return -1;
}
if(pthread_cond_init(&barrier->cond, 0) < 0)
{
pthread_mutex_destroy(&barrier->mutex);
return -1;
}
barrier->tripCount = count;
barrier->count = 0;

return 0;
}

int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
pthread_cond_destroy(&barrier->cond);
pthread_mutex_destroy(&barrier->mutex);
return 0;
}

int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
if(barrier->count >= barrier->tripCount)
{
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
else
{
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
}
}

#endif // PTHREAD_BARRIER_H_
#endif // __APPLE__
Loading

0 comments on commit 7667099

Please sign in to comment.