Commit 6e51ca5f authored by Kento HASEGAWA's avatar Kento HASEGAWA

Merge branch 'master' into adc2019-system

parents c5899bf5 27e81514
......@@ -16,14 +16,15 @@ make
# ToDo
* ブロックを詰める作業の精度向上(現状,右下にスペースが生まれるような結果になりやすい)
* ~~ブロックを詰める作業の精度向上(現状,右下にスペースが生まれるような結果になりやすい)~~ →分割手法導入のため優先度低
* ~~端点を含まないブロックへの対応~~
* ~~組み込みシステム(PYNQ)上での実行→Python3からsolverを呼び出すコードを開発~~
* Ubuntu上でソルバを動作させたときの実行時間計測(NotePC, PYNQ-Z1)
* ~~Ubuntu上でソルバを動作させたときの実行時間計測(NotePC, PYNQ-Z1)~~
* ~~ソルバのハードウェア化~~
* ハードウェアを呼び出すためのwrapperコードを開発(現状jupyter上での実行)
* **ソルバの高速化**
* 求解途中であっても解を出力できるようにする
* ~~ハードウェアを呼び出すためのwrapperコードを開発(現状jupyter上での実行)~~
* ~~**ソルバの高速化**~~
* ~~求解途中であっても解を出力できるようにする~~ →**各種パラメタの調整**(そこそこの時間でそこそこの解を導出するソルバ)
* **例外処理**
* **自作問題の作成**
# Update log
......
This source diff could not be displayed because it is too large. You can view the blob instead.
# Design method
**TODO**
\ No newline at end of file
set_property PACKAGE_PIN R14 [get_ports {led[0]}]
set_property PACKAGE_PIN P14 [get_ports {led[1]}]
set_property PACKAGE_PIN N16 [get_ports {led[2]}]
set_property PACKAGE_PIN M14 [get_ports {led[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]
This diff is collapsed.
#ifndef __PARAM_HPP__
#define __PARAM_HPP__
#define MAX_BLOCKS 128
#define MAX_LINES 256
#define MAX_PATH 128
#define MAX_CELLS 16384 // 128x128
#define MAX_SIZE 128
#define MAX_SIZE_BIT 7
#define SLOT_MAX_SIZE 64
#define ROUND_LIMIT 4096 //32768 // Max=65534(=2^16-2)
//#define PRINT_BOARD
//#define PRINT_SEARCH // for router debug
#define PQ_BIT 12
#define MAX_PQ 4096
#define PRIO_BIT 16
#define DATA_BIT 16
#define ELEM_BIT 32 // ELEM_BIT = PRIO_BIT + DATA_BIT
#define DATA_MASK 65535 // 0000 FFFF
#define DATA_MAX 65535 // FFFF
#define PRIO_MAX 65535 // FFFF
#define LE 1
#define TO 2
#define RI 3
#define BO 4
#define SA_O 1000
#define SA_I 100000
#define TEMP_S 500
#define TEMP_E 0.1
#define INTER_BLOCK_MARGIN 2
#define LOOP 50
#define TRY_LIMIT 500
#define NO_MOVE 10
#define LFSR_RAND_MAX 4294967295
#endif /* __PARAM_HPP__ */
This diff is collapsed.
#ifndef __SOLVER_HPP__
#define __SOLVER_HPP__
//#define DEBUG_PRINT
#include <iostream>
#include "param.hpp"
#include <ap_int.h>
#include <hls_stream.h>
#ifdef DEBUG_PRINT
#include "tools.hpp"
#endif
using namespace std;
bool solver(hls::stream<ap_uint<4> >& state, unsigned int seed[3], short int *pbnum, short int *plnum, short int block_info[MAX_BLOCKS+1][5][3], short int *pwi, short int *phi, short int block_place_global[MAX_BLOCKS+1][2], short int opt_result[MAX_CELLS]);
bool local_placement_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[MAX_BLOCKS+1][5][3], short int block_place_global[MAX_BLOCKS+1][2], short int opt_result[MAX_CELLS]);
bool local_placement_2(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[MAX_BLOCKS+1][5][3], short int opt_result[MAX_CELLS]);
int router(short int size_x, short int size_y, short int line_num, short int board_str[MAX_CELLS]);
int search(short int size_x, short int size_y, short int *path_size, unsigned short int path[MAX_PATH], unsigned short int start, unsigned short int goal, unsigned short int w[MAX_CELLS]);
void enqueue(unsigned int pq_nodes[MAX_PQ], unsigned short int queue_priority, unsigned short int queue_data, ap_uint<PQ_BIT> *pq_len, bool *is_empty);
void dequeue(unsigned int pq_nodes[MAX_PQ], unsigned short int *ret_priority, unsigned short int *ret_data, ap_uint<PQ_BIT> *pq_len, bool *is_empty);
#endif /* __SOLVER_HPP__ */
This diff is collapsed.
#ifndef __IO_HPP__
#define __IO_HPP__
#include <iostream>
#include <sstream> // for istringstream
#include <algorithm> // for replace
#include <math.h>
#include "param.hpp"
using namespace std;
void read_problem(short int *W, short int *H, short int *blocks, short int *line_num, short int block_info[][5][3], short int line_info[][2][5]);
void extract_line_info(short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5]);
float calcPlaceCost(short int line_num, short int line_info[MAX_LINES+1][2][5], pair<short int,short int> block_place_global[MAX_BLOCKS+1]);
void global_placement(unsigned int seed[3], short int W, short int H, short int line_num, short int blocks, short int block_info[MAX_BLOCKS+1][5][3], short int line_info[MAX_LINES+1][2][5], short int block_place_global[MAX_BLOCKS+1][2]);
#endif /* __IO_HPP__ */
#include <iostream>
#include <getopt.h>
#include "param.hpp"
#include "io.hpp"
#include "solver.hpp"
#include "tools.hpp"
using namespace std;
void usage() {
cout << "Usage: ./sim.exe [--output output-file] [--seed seed-value] < input-file" << endl;
exit(-1);
}
int main(int argc, char *argv[]) {
char *out_filename = NULL;
uint32_t seed_v = 214;
// Get options
struct option longopts[] = {
{"output", required_argument, NULL, 'o'},
{"seed", required_argument, NULL, 's'},
{0, 0, 0, 0}
};
int opt, optidx;
while((opt = getopt_long(argc, argv, "o:s:", longopts, &optidx)) != -1) {
switch(opt) {
case 'o':
out_filename = optarg;
break;
case 's':
sscanf(optarg, "%u", &seed_v);
break;
default:
usage();
}
}
// Init seed value
uint32_t seed[3];
srand(seed_v);
seed[0] = rand();
seed[1] = rand();
seed[2] = rand();
// W: Max width of a board (specified in a problem file)
// H: Max height of a board (specified in a problem file)
// blocks: # of blocks (specified in a problem file)
short int W, H, blocks, line_num;
// W_ext: Width of a current solution
// H_ext: Height of a current solution
short int W_ext, H_ext;
// block data
short int block_info[MAX_BLOCKS+1][5][3];
// line data
short int line_info[MAX_LINES+1][2][5];
// block positions on slot matrix
short int block_place_global[MAX_BLOCKS+1][2];
// An opt. result will be stored in the array
short int opt_result[MAX_CELLS];
// Check problem
read_problem(&W, &H, &blocks, &line_num, block_info, line_info);
extract_line_info(line_num, blocks, block_info, line_info);
// Solver body
global_placement(seed, W, H, line_num, blocks, block_info, line_info, block_place_global);
hls::stream<ap_uint<4> > state;
bool solved = solver(state, seed, &blocks, &line_num, block_info, &W_ext, &H_ext, block_place_global, opt_result);
// Check answer
if(!solved) {
cout << "Fail re-routing" << endl;
}
else {
cout << "== Answer ==" << endl;
cout << "SIZE " << W_ext << "X" << H_ext << endl;
show_result(line_num, blocks, W_ext, H_ext, opt_result);
}
if(W_ext > W || H_ext > H) {
cout << "Fail satisfying constraint T_T" << endl;
}
else {
cout << "Satisfy constraint ^_^" << endl;
if(out_filename == NULL) return 0;
cout << "Output to " << out_filename << endl;
short int block_place_basis[MAX_BLOCKS+1][2];
for(int i = 1; i <= blocks; i++) {
block_place_basis[i][0] = block_info[i][0][0];
block_place_basis[i][1] = block_info[i][0][1];
}
output_to_file(out_filename, line_num, blocks, W_ext, H_ext, opt_result, block_place_basis);
}
return 0;
}
SIZE 10X10
BLOCK_NUM 8
BLOCK#1 1X4
1
+
8
7
BLOCK#2 3X2
0,8,0
7,6,+
BLOCK#3 2X3
10,0
+,0
3,9
BLOCK#4 2X2
1,2
4,+
BLOCK#5 3X2
11,+,+
0,0,3
BLOCK#6 3X2
0, +,2
5,11,0
BLOCK#7 3X2
0,10,6
9, 5,0
BLOCK#8 3X2
+,+,0
0,+,4
\ No newline at end of file
#include "tools.hpp"
void output_to_file(char *f_name, short int line_num, short int blocks, short int wi, short int hi, short int board[], short int block_place[][2]) {
ofstream outputfile(f_name);
outputfile << "SIZE " << wi << "X" << hi << endl;
for(short int q = 0; q < hi; q++) {
for(short int p = 0; p < wi; p++) {
short int n = board[q*MAX_SIZE+p];
if(n == 0) { outputfile << 0; }
else if(n == -1) { outputfile << 0; }
else{ outputfile << n; }
if(p != wi-1) { outputfile << ", "; }
}
outputfile << endl;
}
outputfile << setfill(' ');
outputfile << dec;
for(int i = 1; i <= blocks; i++) {
outputfile << "BLOCK#" << i << " @(" << block_place[i][0] << "," << block_place[i][1] << ")" << endl;
}
outputfile.close();
}
void show_result(short int line_num, short int blocks, short int wi, short int hi, short int board[]) {
// This shows a number from 1 to 255
for(short int q = 0; q < hi; q++) {
for(short int p = 0; p < wi; p++) {
short int n = board[q*MAX_SIZE+p];
if(n == 0) { cout << "--"; }
else if(n == -1) { cout << "XX"; }
else{ cout << setfill('0') << setw(2) << hex << n; }
if(p != wi-1) { cout << " "; }
}
cout << endl;
}
cout << setfill(' ');
cout << dec;
}
#ifndef __TOOLS_HPP__
#define __TOOLS_HPP__
using namespace std;
#include <iostream>
#include <iomanip> // for setw
#include <fstream>
#include "param.hpp"
void usage();
void output_to_file(char *f_name, short int line_num, short int blocks, short int wi, short int hi, short int board[], short int block_place[][2]);
void show_result(short int line_num, short int blocks, short int wi, short int hi, short int board[]);
#endif /* __TOOLS_HPP__ */
# Environment
* PYNQ-Z1 (v2.4) (Release 2019_02_21)
* PYNQ Linux, based on Ubuntu 18.04
* Python 3.6.5
# Requirements
* Hardware solver (Latest ver. `190826_solver`) generated [here](https://www.togawa.cs.waseda.ac.jp/gitlab/adc2019/kawamura-solver/tree/master/hardware_design)
* Python module `MySolverModule` generated [here](https://www.togawa.cs.waseda.ac.jp/gitlab/adc2019/kawamura-solver/tree/master/solver_python)
# Usage
```
sudo python3 main.py -p input-Q-file(.txt) [-s seed-value] [-o output-A-file(.txt)]
```
* When running the script, 3 LEDs (LD0-LD2) are turned on & off as follows:
|(LD2, LD1, LD0)|state|
|:---:|:---|
|(0, 0, 0)|overlay loaded|
|(0, 0, 1)|start|
|(0, 1, 1)|1st routing completed|
|(0, 1, 0)|1st routing failed|
|(1, 1, 1)|2nd routing completed|
|(1, 0, 0)|2nd routing failed|
# Results
* 19.08.26: sample_Q.txt
|seed value|cost|final state|solution|real time|
|:---:|:---:|:---:|:---:|:---:|
|0|66 |(1, 1, 1)|15X11|51[sec.]|
|1|66 |(1, 1, 1)|15X11|51[sec.]|
|2|54.5|(1, 1, 1)| 7X12|18[sec.]|
|3|54.5|(1, 1, 1)| 8X10|15[sec.]|
|4|54.5|(1, 1, 1)| 8X11|14[sec.]|
|5|54.5|(1, 1, 1)| 9X10|12[sec.]|
|6|54.5|(1, 1, 1)| 9X11|15[sec.]|
|7|56 |(0, 1, 0)| N/A |14[sec.]|
|8|55 |(1, 0, 0)| N/A |37[sec.]|
|9|54.5|(1, 1, 1)| 8X12|15[sec.]|
import numpy as np
from argparse import ArgumentParser
import time
import mySolverModule
from pynq import Overlay
from pynq import Xlnk
from pynq import MMIO
def commandParsing():
parser = ArgumentParser()
parser.add_argument('-p', '--problem', help='Problem file name', required=True) # Required
parser.add_argument('-s', '--seed', help='seed value') # Option
parser.add_argument('-o', '--output', help='Output file name') # Option
args = vars(parser.parse_args())
return args
def main(args):
print('Download overlay ...', flush=True)
OL = Overlay('/home/xilinx/pynq/overlays/190826_solver/design_1.bit')
ip = 'solver_0'
print(hex(OL.ip_dict[ip]['phys_addr']))
seed_v = 3
if args['seed'] is not None: seed_v = int(args['seed'])
seed = mySolverModule.seed_generator(seed_v) # ndarray
info = mySolverModule.check_problem(args['problem']) # extract info (block, line, ...)
## Global placement
p = mySolverModule.placer(seed, info['W'][0], info['H'][0], info['blocks'][0], info['line_num'][0], info['block_info'], info['line_info'])
print(p['block_place'][1:info['blocks'][0]])
## Allocate contiguous blocks
xlnk = Xlnk()
print(xlnk.cma_stats())
seed_buf = xlnk.cma_array(shape=(3,), dtype=np.uint32)
B_num_buf = xlnk.cma_array(shape=(1,), dtype=np.int16)
L_num_buf = xlnk.cma_array(shape=(1,), dtype=np.int16)
B_info_buf = xlnk.cma_array(shape=(129,5,3), dtype=np.int16)
W_ext_buf = xlnk.cma_array(shape=(1,), dtype=np.int16)
H_ext_buf = xlnk.cma_array(shape=(1,), dtype=np.int16)
B_place_buf = xlnk.cma_array(shape=(129,2), dtype=np.int16)
result_buf = xlnk.cma_array(shape=(16384,), dtype=np.int16)
print(xlnk.cma_stats())
xlnk.cma_memcopy(seed_buf, p['seed'], 12)
xlnk.cma_memcopy(B_num_buf, info['blocks'], 2)
xlnk.cma_memcopy(L_num_buf, info['line_num'], 2)
xlnk.cma_memcopy(B_info_buf, info['block_info'], 3870)
xlnk.cma_memcopy(B_place_buf, p['block_place'], 516)
phy_addr_0 = seed_buf.physical_address
phy_addr_1 = B_num_buf.physical_address
phy_addr_2 = L_num_buf.physical_address
phy_addr_3 = B_info_buf.physical_address
phy_addr_4 = W_ext_buf.physical_address
phy_addr_5 = H_ext_buf.physical_address
phy_addr_6 = B_place_buf.physical_address
phy_addr_7 = result_buf.physical_address
print(hex(phy_addr_0), hex(phy_addr_1), hex(phy_addr_2), hex(phy_addr_3))
print(hex(phy_addr_4), hex(phy_addr_5), hex(phy_addr_6), hex(phy_addr_7))
## Write pointers
lite_base_addr = OL.ip_dict[ip]['phys_addr'] # IP_BASE_ADDRESS
lite_size = OL.ip_dict[ip]['addr_range'] # ADDRESS_RANGE
mmio = MMIO(lite_base_addr, lite_size)
mmio.write(0x18, phy_addr_0)
mmio.write(0x20, phy_addr_1)
mmio.write(0x28, phy_addr_2)
mmio.write(0x30, phy_addr_3)
mmio.write(0x38, phy_addr_4)
mmio.write(0x40, phy_addr_5)
mmio.write(0x48, phy_addr_6)
mmio.write(0x50, phy_addr_7)
## Local placement
print('Solver start ...', flush=True)
mmio.write(0x00, 1)
while mmio.read(0x00) != 6:
time.sleep(0.1)
print(bin(mmio.read(0x00)))
print(mmio.read(0x10))
if mmio.read(0x10) == 0:
print('Fail routing m(_ _)m')
return # End function
W_ext = np.zeros((1,), dtype=np.int16)
H_ext = np.zeros((1,), dtype=np.int16)
result = np.zeros((16384,), dtype=np.int16)
xlnk.cma_memcopy(W_ext, W_ext_buf, 2)
xlnk.cma_memcopy(H_ext, H_ext_buf, 2)
xlnk.cma_memcopy(result, result_buf, 32768)
xlnk.cma_memcopy(info['block_info'], B_info_buf, 3870)
mySolverModule.show_result(W_ext[0], H_ext[0], info['blocks'][0], info['line_num'][0], info['block_info'], result)
if W_ext[0] > info['W'][0] or H_ext[0] > info['H'][0]:
print('Fail satisfying constraint T_T')
else:
print('Satisfy constraint ^_^')
if args['output'] is not None: mySolverModule.output_to_file(args['output'], W_ext[0], H_ext[0], info['blocks'][0], info['line_num'][0], info['block_info'], result)
if __name__ == '__main__':
args = commandParsing()
main(args)
SIZE 8X10
1, 1, 1, 2, 2, 2, 2, 0
0, 0, 4, 0, 0, 0, 2, 2
8, 8, 4, 4, 4, 0, 4, 2
7, 8, 0, 0, 4, 4, 4, 2
7, 6, 0, 0, 0, 0, 2, 2
0, 6, 6, 6, 5, 11, 0, 0
10, 10, 10, 6, 5, 11, 0, 0
0, 9, 5, 5, 5, 0, 0, 3
3, 9, 3, 3, 3, 3, 3, 3
3, 3, 3, 0, 0, 0, 0, 0
BLOCK#1 @(0,0)
BLOCK#2 @(0,3)
BLOCK#3 @(0,6)
BLOCK#4 @(2,0)
BLOCK#5 @(5,6)
BLOCK#6 @(4,4)
BLOCK#7 @(1,6)
BLOCK#8 @(4,1)
SIZE 10X10
BLOCK_NUM 8
BLOCK#1 1X4
1
+
8
7
BLOCK#2 3X2
0,8,0
7,6,+
BLOCK#3 2X3
10,0
+,0
3,9
BLOCK#4 2X2
1,2
4,+
BLOCK#5 3X2
11,+,+
0,0,3
BLOCK#6 3X2
0, +,2
5,11,0
BLOCK#7 3X2
0,10,6
9, 5,0
BLOCK#8 3X2
+,+,0
0,+,4
\ No newline at end of file
TARGET = sim
OBJS = $(CPPS:.cpp=.o)
CPPS = main.cpp io.cpp solver.cpp tools.cpp
CXX = arm-linux-gnueabihf-g++
CXXFLAGS = -std=c++11 -O3
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) -o $@ $(OBJS)
clean:
rm *.o
rm $(TARGET)
......@@ -29,7 +29,7 @@
#define RI 3
#define BO 4
#define SA_O 1000
#define SA_O 100
#define SA_I 100000
#define TEMP_S 500
#define TEMP_E 0.1
......
/// py_wrapper.cpp ///
// #include <Python.h>
#include "/usr/include/python3.5m/Python.h"
#include "/usr/include/python3.5m/numpy/arrayobject.h"
#include "/usr/include/python3.5m/numpy/arrayscalars.h"
#include "/usr/include/python3.6m/Python.h"
#include "/usr/include/python3.6m/numpy/arrayobject.h"
#include "/usr/include/python3.6m/numpy/arrayscalars.h"
#include <iostream>
#include <fstream>
......@@ -199,6 +199,80 @@ static PyObject* check_problem_wrapper(PyObject* self, PyObject* args) {
return dict;
}
static PyObject* placer_wrapper(PyObject* self, PyObject* args) {
uint32_t seed[3];
short int W, H, blocks, line_num;
short int block_info[MAX_BLOCKS+1][5][3];
short int line_info[MAX_LINES+1][2][5];
PyObject* a_seed;
PyObject* a_block_info;
PyObject* a_line_info;
// PyObject (ndarray, short int, short int, short int, short int, ndarray, ndarray)
if(!PyArg_ParseTuple(args, "O!hhhhO!O!", &PyArray_Type, &a_seed, &W, &H, &blocks, &line_num, &PyArray_Type, &a_block_info, &PyArray_Type, &a_line_info)) {
return NULL;
}
seed[0] = *(uint32_t*)PyArray_GETPTR1(a_seed, 0);
seed[1] = *(uint32_t*)PyArray_GETPTR1(a_seed, 1);
seed[2] = *(uint32_t*)PyArray_GETPTR1(a_seed, 2);
for(npy_intp i = 0; i < MAX_BLOCKS+1; i++) {
for(npy_intp j = 0; j < 5; j++) {
for(npy_intp k = 0; k < 3; k++) {
block_info[i][j][k] = *(short int*)PyArray_GETPTR3(a_block_info, i, j, k);
}
}
}
for(npy_intp i = 0; i < MAX_LINES+1; i++) {
for(npy_intp j = 0; j < 2; j++) {
for(npy_intp k = 0; k < 5; k++) {
line_info[i][j][k] = *(short int*)PyArray_GETPTR3(a_line_info, i, j, k);
}
}
}
// block positions on slot matrix
short int block_place_global[MAX_BLOCKS+1][2];
placer(seed, W, H, blocks, line_num, block_info, line_info, block_place_global);
// create dictionary
PyObject* dict = PyDict_New();
// seed //
PyObject* seed_key = PyUnicode_FromString("seed");
*(uint32_t*)PyArray_GETPTR1(a_seed, 0) = seed[0];
*(uint32_t*)PyArray_GETPTR1(a_seed, 1) = seed[1];
*(uint32_t*)PyArray_GETPTR1(a_seed, 2) = seed[2];
PyDict_SetItem(dict, seed_key, a_seed);
// block_info //
PyObject* block_place_key = PyUnicode_FromString("block_place");
int ndim = 2;
npy_intp *s = (npy_intp*)malloc(ndim * sizeof(npy_intp));
s[0] = MAX_BLOCKS+1;
s[1] = 2;
PyObject* block_place_list = PyArray_SimpleNew(ndim, s, NPY_INT16); // ndarray
free(s);
for(npy_intp i = 0; i < MAX_BLOCKS+1; i++) {
*(short int*)PyArray_GETPTR2(block_place_list, i, 0) = block_place_global[i][0];
*(short int*)PyArray_GETPTR2(block_place_list, i, 1) = block_place_global[i][1];
}
PyDict_SetItem(dict, block_place_key, block_place_list);
// PyObject (dictionary)
return dict;
}
static PyObject* solver_wrapper(PyObject* self, PyObject* args) {
uint32_t seed[3];
......@@ -329,7 +403,7 @@ static PyObject* show_result_wrapper(PyObject* self, PyObject* args) {
}
}
}
for(npy_intp i = 0; i < MAX_BLOCKS+1; i++) {
for(npy_intp i = 0; i < MAX_CELLS; i++) {
opt_result[i] = *(short int*)PyArray_GETPTR1(a_opt_result, i);
}
......@@ -366,7 +440,7 @@ static PyObject* output_to_file_wrapper(PyObject* self, PyObject* args) {
}
}
}
for(npy_intp i = 0; i < MAX_BLOCKS+1; i++) {
for(npy_intp i = 0; i < MAX_CELLS; i++) {
opt_result[i] = *(short int*)PyArray_GETPTR1(a_opt_result, i);
}
......@@ -385,6 +459,7 @@ static PyMethodDef mySolverMethods[] = {
{"test_func", test_func_wrapper, METH_VARARGS, "test function"},
{"seed_generator", seed_generator_wrapper, METH_VARARGS, "generate seed values"},
{"check_problem", check_problem_wrapper, METH_VARARGS, "read problem"},
{"placer", placer_wrapper, METH_VARARGS, "placer"},
{"solver", solver_wrapper, METH_VARARGS, "solver"},
{"show_result", show_result_wrapper, METH_VARARGS, "show result"},
{"output_to_file", output_to_file_wrapper, METH_VARARGS, "output result to file"},
......
......@@ -7,7 +7,7 @@ int solver(uint32_t seed[3], short int W, short int H, short int blocks, short i
lfsr_random_init(seed[0], seed[1], seed[2]);
// block positions on slot matrix
pair<short int,short int> block_place_global[MAX_BLOCKS+1];
short int block_place_global[MAX_BLOCKS+1][2];
while(1) {
global_placement(W, H, line_num, blocks, block_info, line_info, block_place_global);
......@@ -19,6 +19,18 @@ int solver(uint32_t seed[3], short int W, short int H, short int blocks, short i
return 0;
}
int placer(uint32_t seed[3], short int W, short int H, short int blocks, short int line_num, short int block_info[][5][3], short int line_info[][2][5], short int block_place_global[][2]) {
lfsr_random_init(seed[0], seed[1], seed[2]);
global_placement(W, H, line_num, blocks, block_info, line_info, block_place_global);
seed[0] = lfsr;
seed[1] = lfsr_x;
seed[2] = lfsr_y;
return 0;
}
void lfsr_random_init(uint32_t seed, uint32_t seed_x, uint32_t seed_y) {
lfsr = seed;
lfsr_x = seed_x;
......@@ -59,7 +71,7 @@ uint32_t lfsr_y_random() {
return lfsr_y;
}
float calcPlaceCost(short int line_num, short int line_info[][2][5], pair<short int,short int> block_place_global[]) {
float calcPlaceCost(short int line_num, short int line_info[][2][5], short int block_place_global[][2]) {
short int block1, block2; // block idx
short int x1, y1; // 1st block
......@@ -77,10 +89,10 @@ float calcPlaceCost(short int line_num, short int line_info[][2][5], pair<short
for(int l = 1; l <= line_num; l++) { // Calc cost
block1 = line_info[l][0][0]; // 1st block including line#L
block2 = line_info[l][1][0]; // 2nd block including line#L
x1 = block_place_global[block1].first;
y1 = block_place_global[block1].second;
x2 = block_place_global[block2].first;
y2 = block_place_global[block2].second;
x1 = block_place_global[block1][0];
y1 = block_place_global[block1][1];
x2 = block_place_global[block2][0];
y2 = block_place_global[block2][1];
delta_x = x1 - x2;
delta_y = y1 - y2;
dist_x = abs(delta_x);
......@@ -112,7 +124,7 @@ float calcPlaceCost(short int line_num, short int line_info[][2][5], pair<short
return cost;
}
void global_placement(short int W, short int H, short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5], pair<short int,short int> block_place_global[]) {
void global_placement(short int W, short int H, short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5], short int block_place_global[][2]) {
cout << "== Global placement ==" << endl;
short int sx_sum = 0, sy_sum = 0;
......@@ -136,9 +148,9 @@ void global_placement(short int W, short int H, short int line_num, short int bl
for(short int q = 0; q <= max_y; q++) {
for(short int p = 0; p <= max_x; p++) {
if(i <= blocks) {
block_place_global[i] = make_pair(p, q);
block_place_global[i][0] = p;
block_place_global[i][1] = q;
global_slot[q][p] = i;
//cout << "Block#" << i << ": (" << block_place_global[i].first << ", " << block_place_global[i].second << ")" << endl;
i++;
}
else {
......@@ -164,28 +176,28 @@ void global_placement(short int W, short int H, short int line_num, short int bl
// i_x, i_y, j_x, j_y
for(int counter_in = 0; counter_in < SA_I; counter_in++) {
short int trgt_i = lfsr_random() % blocks + 1;
short int i_x = block_place_global[trgt_i].first;
short int i_y = block_place_global[trgt_i].second;
short int i_x = block_place_global[trgt_i][0];
short int i_y = block_place_global[trgt_i][1];
short int j_x = lfsr_x_random() % SLOT_MAX_SIZE;
short int j_y = lfsr_y_random() % SLOT_MAX_SIZE;
if(j_x > max_x || j_y > max_y) continue;
short int trgt_j = global_slot[j_y][j_x]; // slot -> block
block_place_global[trgt_i].first = j_x;
block_place_global[trgt_i].second = j_y;
block_place_global[trgt_i][0] = j_x;
block_place_global[trgt_i][1] = j_y;
global_slot[j_y][j_x] = trgt_i;
block_place_global[trgt_j].first = i_x;
block_place_global[trgt_j].second = i_y;
block_place_global[trgt_j][0] = i_x;
block_place_global[trgt_j][1] = i_y;
global_slot[i_y][i_x] = trgt_j;
float new_cost = calcPlaceCost(line_num, line_info, block_place_global);
if(new_cost > cost && (float)lfsr_random()/LFSR_RAND_MAX > expf(-(new_cost-cost)/temperature)) {
block_place_global[trgt_i].first = i_x;
block_place_global[trgt_i].second = i_y;
block_place_global[trgt_i][0] = i_x;
block_place_global[trgt_i][1] = i_y;
global_slot[i_y][i_x] = trgt_i;
block_place_global[trgt_j].first = j_x;
block_place_global[trgt_j].second = j_y;
block_place_global[trgt_j][0] = j_x;
block_place_global[trgt_j][1] = j_y;
global_slot[j_y][j_x] = trgt_j;
}
else if(new_cost != cost) {
......@@ -215,7 +227,7 @@ pair<short int,short int> sub_pair(pair<short int,short int> x, pair<short int,s
return make_pair(x.first-y.first, x.second-y.second);
}
bool local_placement_with_routing_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], pair<short int,short int> block_place_global[], short int opt_result[]) {
bool local_placement_with_routing_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], short int block_place_global[][2], short int opt_result[]) {
short int wi, hi; // board size (w, h)
......@@ -234,8 +246,8 @@ bool local_placement_with_routing_1(short int line_num, short int blocks, short
// load a placement result
short int min_x = SLOT_MAX_SIZE, min_y = SLOT_MAX_SIZE, max_x = 0, max_y = 0;
for(int i = 1; i <= blocks; i++) {
short int p = block_place_global[i].first;
short int q = block_place_global[i].second;
short int p = block_place_global[i][0];
short int q = block_place_global[i][1];
if(p < min_x) { min_x = p; }
if(q < min_y) { min_y = q; }
if(p > max_x) { max_x = p; }
......@@ -250,8 +262,8 @@ bool local_placement_with_routing_1(short int line_num, short int blocks, short
for(int i = 1; i <= blocks; i++) {
short int sx = block_info[i][0][0];
short int sy = block_info[i][0][1];
short int p = block_place_global[i].first;
short int q = block_place_global[i].second;
short int p = block_place_global[i][0];
short int q = block_place_global[i][1];
if(sx > slot_w[p]) { slot_w[p] = sx; }
if(sy > slot_h[q]) { slot_h[q] = sy; }
}
......@@ -273,8 +285,8 @@ bool local_placement_with_routing_1(short int line_num, short int blocks, short
for(int i = 1; i <= blocks; i++) {
short int sx = block_info[i][0][0];
short int sy = block_info[i][0][1];
short int p = block_place_global[i].first;
short int q = block_place_global[i].second;
short int p = block_place_global[i][0];
short int q = block_place_global[i][1];
block_place_basis[i] = make_pair(slot_w_t[p], slot_h_t[q]);
block_place_slack[i][0] = slot_w[p] - sx;
block_place_slack[i][1] = slot_h[q] - sy;
......
#ifndef __SOLVER_HPP__
#define __SOLVER_HPP__
#include "/usr/include/python3.5m/Python.h"
#include "/usr/include/python3.6m/Python.h"
#include <iostream>
#include <math.h>
......@@ -12,15 +12,16 @@
using namespace std;
int solver(uint32_t seed[3], short int W, short int H, short int blocks, short int line_num, short int block_info[][5][3], short int line_info[][2][5], short int *pwi, short int *phi, short int opt_result[]);
int placer(uint32_t seed[3], short int W, short int H, short int blocks, short int line_num, short int block_info[][5][3], short int line_info[][2][5], short int block_place_global[][2]);
void lfsr_random_init(uint32_t seed, uint32_t seed_x, uint32_t seed_y);
uint32_t lfsr_random();
uint32_t lfsr_x_random();
uint32_t lfsr_y_random();
void global_placement(short int W, short int H, short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5], pair<short int,short int> block_place_global[]);
float calcPlaceCost(short int line_num, short int line_info[][2][5], pair<short int,short int> block_place_global[]);
bool local_placement_with_routing_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], pair<short int,short int> block_place_global[], short int opt_result[]);
void global_placement(short int W, short int H, short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5], short int block_place_global[][2]);
float calcPlaceCost(short int line_num, short int line_info[][2][5], short int block_place_global[][2]);
bool local_placement_with_routing_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], short int block_place_global[][2], short int opt_result[]);
bool local_placement_with_routing_2(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], short int opt_result[]);
pair<short int,short int> add_pair(pair<short int,short int> x, pair<short int,short int> y);
pair<short int,short int> sub_pair(pair<short int,short int> x, pair<short int,short int> y);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment