ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
zlayout.cpp
Go to the documentation of this file.
1
5
6#include <zlayout/zlayout.hpp>
7#include <iostream>
8#include <stdexcept>
9#include <chrono>
10#include <algorithm>
11#include <string>
12#include <cstdio>
13#include <cstdlib>
14
15#ifdef ZLAYOUT_OPENMP
16#include <omp.h>
17#endif
18
19namespace zlayout {
20
21namespace {
22 bool g_initialized = false;
23 bool g_openmp_enabled = false;
24}
25
26const char* get_version() {
27 return Version::STRING;
28}
29
30bool initialize(bool enable_openmp) {
31 if (g_initialized) {
32 std::cerr << "Warning: ZLayout already initialized" << std::endl;
33 return true;
34 }
35
36 try {
37 // Initialize OpenMP if requested and available
38 g_openmp_enabled = false;
39 if (enable_openmp) {
40#ifdef ZLAYOUT_OPENMP
41 // Set OpenMP number of threads
42 int num_threads = omp_get_max_threads();
43 std::cout << "ZLayout: OpenMP enabled with " << num_threads << " threads" << std::endl;
44 g_openmp_enabled = true;
45#else
46 std::cout << "ZLayout: OpenMP not available in this build" << std::endl;
47#endif
48 }
49
50 // Perform any other initialization
51 std::cout << "ZLayout v" << get_version() << " initialized successfully" << std::endl;
52 std::cout << "Features enabled:" << std::endl;
53 std::cout << " - High-precision geometry (tolerance: " << geometry::Point::TOLERANCE << ")" << std::endl;
54 std::cout << " - Quadtree spatial indexing" << std::endl;
55 std::cout << " - Sharp angle detection" << std::endl;
56 std::cout << " - Edge intersection detection" << std::endl;
57 std::cout << " - Narrow distance analysis" << std::endl;
58
59 if (g_openmp_enabled) {
60 std::cout << " - OpenMP parallel processing" << std::endl;
61 }
62
63 g_initialized = true;
64 return true;
65
66 } catch (const std::exception& e) {
67 std::cerr << "Error initializing ZLayout: " << e.what() << std::endl;
68 return false;
69 }
70}
71
72void cleanup() {
73 if (!g_initialized) {
74 return;
75 }
76
77 // Perform cleanup operations
78 g_initialized = false;
79 g_openmp_enabled = false;
80
81 std::cout << "ZLayout cleanup completed" << std::endl;
82}
83
85 return g_initialized;
86}
87
89 return g_openmp_enabled;
90}
91
92// Performance utilities
93
95 std::chrono::high_resolution_clock::time_point start_time;
96
98 start_time = std::chrono::high_resolution_clock::now();
99 }
100
101 double elapsed_ms() const {
102 auto end_time = std::chrono::high_resolution_clock::now();
103 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
104 return duration.count() / 1000.0;
105 }
106
107 double elapsed_us() const {
108 auto end_time = std::chrono::high_resolution_clock::now();
109 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
110 return static_cast<double>(duration.count());
111 }
112};
113
114// Library information functions
115
117 std::string compiler;
118 std::string build_type;
119 std::string platform;
122};
123
124// Helper function for MSVC 2012 compatibility
125namespace {
126 std::string int_to_string(int value) {
127#if defined(_MSC_VER) && _MSC_VER < 1800 // MSVC 2012 and earlier
128 char buffer[32];
129 sprintf(buffer, "%d", value);
130 return std::string(buffer);
131#else
132 return std::to_string(value);
133#endif
134 }
135}
136
138 SystemInfo info;
139
140 // Compiler information
141#ifdef __GNUC__
142 info.compiler = "GCC " + int_to_string(__GNUC__) + "." + int_to_string(__GNUC_MINOR__);
143#elif defined(__clang__)
144 info.compiler = "Clang " + int_to_string(__clang_major__) + "." + int_to_string(__clang_minor__);
145#elif defined(_MSC_VER)
146 info.compiler = "MSVC " + int_to_string(_MSC_VER);
147#else
148 info.compiler = "Unknown compiler";
149#endif
150
151 // Build type
152#ifdef DEBUG
153 info.build_type = "Debug";
154#else
155 info.build_type = "Release";
156#endif
157
158 // Platform
159#ifdef _WIN32
160 info.platform = "Windows";
161#elif defined(__linux__)
162 info.platform = "Linux";
163#elif defined(__APPLE__)
164 info.platform = "macOS";
165#else
166 info.platform = "Unknown";
167#endif
168
169 // OpenMP support
170#ifdef ZLAYOUT_OPENMP
171 info.openmp_support = true;
172 info.max_threads = omp_get_max_threads();
173#else
174 info.openmp_support = false;
175 info.max_threads = 1;
176#endif
177
178 return info;
179}
180
182 auto info = get_system_info();
183
184 std::cout << "\n=== ZLayout System Information ===" << std::endl;
185 std::cout << "Version: " << get_version() << std::endl;
186 std::cout << "Compiler: " << info.compiler << std::endl;
187 std::cout << "Build Type: " << info.build_type << std::endl;
188 std::cout << "Platform: " << info.platform << std::endl;
189 std::cout << "OpenMP Support: " << (info.openmp_support ? "Yes" : "No") << std::endl;
190 std::cout << "Max Threads: " << info.max_threads << std::endl;
191 std::cout << "Geometry Tolerance: " << geometry::Point::TOLERANCE << std::endl;
192 std::cout << "=================================" << std::endl;
193}
194
195// Error handling utilities
196
197class ZLayoutException : public std::runtime_error {
198public:
199 explicit ZLayoutException(const std::string& message)
200 : std::runtime_error("ZLayout Error: " + message) {}
201};
202
204 if (!g_initialized) {
205 throw ZLayoutException("Library not initialized. Call zlayout::initialize() first.");
206 }
207}
208
209// Memory and performance monitoring
210
216
217// Simple memory tracker (for debug builds)
218#ifdef DEBUG
219namespace {
220 MemoryInfo g_memory_info = {0, 0, 0};
221}
222
223void* tracked_malloc(size_t size) {
224 void* ptr = std::malloc(size);
225 if (ptr) {
226 g_memory_info.allocated_bytes += size;
227 g_memory_info.peak_bytes = std::max(g_memory_info.peak_bytes, g_memory_info.allocated_bytes);
228 g_memory_info.allocation_count++;
229 }
230 return ptr;
231}
232
233void tracked_free(void* ptr, size_t size) {
234 if (ptr) {
235 std::free(ptr);
236 g_memory_info.allocated_bytes -= size;
237 }
238}
239
241 return g_memory_info;
242}
243#else
245 return {0, 0, 0}; // Not tracked in release builds
246}
247#endif
248
249} // namespace zlayout
ZLayoutException(const std::string &message)
Definition zlayout.cpp:199
static constexpr double TOLERANCE
Default precision tolerance for floating point comparisons.
Definition point.hpp:29
Main namespace for ZLayout library.
Definition component.hpp:20
bool initialize(bool enable_openmp=true)
Initialize ZLayout library.
Definition zlayout.cpp:30
void cleanup()
Cleanup ZLayout library resources.
Definition zlayout.cpp:72
void assert_initialized()
Definition zlayout.cpp:203
const char * get_version()
Get library version string.
Definition zlayout.cpp:26
bool is_initialized()
Definition zlayout.cpp:84
SystemInfo get_system_info()
Definition zlayout.cpp:137
bool is_openmp_enabled()
Definition zlayout.cpp:88
MemoryInfo get_memory_info()
Definition zlayout.cpp:244
void print_system_info()
Definition zlayout.cpp:181
double elapsed_ms() const
Definition zlayout.cpp:101
std::chrono::high_resolution_clock::time_point start_time
Definition zlayout.cpp:95
double elapsed_us() const
Definition zlayout.cpp:107
std::string compiler
Definition zlayout.cpp:117
std::string platform
Definition zlayout.cpp:119
std::string build_type
Definition zlayout.cpp:118
static constexpr const char * STRING
Definition zlayout.hpp:33
ZLayout - Advanced Electronic Design Automation Layout Library.