Theoretical Basis and Implementation Mechanism of the Programming Platform for Ternary Optical Computer

This paper presents a method for building a programming platform for ternary optical computer(TOC). Firstly, the reasons why the existing programming platform can not be directly applied to the ternary optical computer are analyzed. Then, the theoretical basis and core technologies for building the programming platform of the ternary optical computer are given, including: building a model—simple structured data type computer to express the application characteristics of the ternary optical computer, building an operation-data file containing data and computational requirements, expanding the operation-data file transfer instructions. According to our proposed theory, the implementation mechanism of the TOC programming platform is constructed. Finally, the effectiveness of the programming platform is verified by experiments. The programming platform simplifies the application of the TOC and bridges the gap between the user and the TOC.


I. INTRODUCTION
As electronic computers have matured since their inception, scientists have been exploring and developing other types of computers. In particular, considering that the physical characteristics of electrons will limit the development of electronic computer hardware, various non-electronic computers have emerged, such as optical computers [1], [2], quantum computers [3], [4], biological computers [5], etc. Research on ternary optical computers is a unique branch of nontraditional computer research, with France, India, and Iraq currently engaged in many aspects of international research on ternary optical computers. The second and third generation experimental systems of the ternary optical computer, ShangDa11 (for short SD11, means:Shanghai University 2011), developed by the Optical Computer Laboratory of Shanghai University, both have reconfigurable ternary logical The associate editor coordinating the review of this manuscript and approving it for publication was Md Selim Habib . optical processors with thousand-bit data widths and easily scalable bit counts. The completed experiments fully confirm this theory of large number of computer data bits and processor reconfigurability. The first stably operating ternary optical computer -SD16 (Shanghai University 2016), has 192 ternary logic bits in one basic arithmetic module and can construct up to a 36-bit three-step parallel binary adder or a 64-bit one-step parallel binary adder.
Ternary optical computers are named for their processors that use three-state optical signals to represent information and are capable of performing all ternary logic operations [6], [7]. The biggest difference between this processor in application and conventional electronic processors is that it allows the user to reconfigure the computational functions of individual data bits at any time, and a huge number of data bits can be used independently in any group [8]- [11]. In addition, the memory of a ternary optical computer has a data bus width that varies with the total number of processed data bits on the processor-facing side, but the effect of this VOLUME 10, 2022 This work is licensed under a Creative Commons Attribution 4.0 License. For more information, see https://creativecommons.org/licenses/by/4.0/ feature is not seen at the application level [12]. These dictate that current programming environments built on conventional processors cannot produce applications that take advantage of the characteristics of ternary optical processors. Since the functions of the I/O components and other parts of the ternary optical computer are no different from those of the conventional computer, which is the background device for building the new computer, the ternary optical computer follows exactly the conventional device in these aspects and contains a front-end PC for peripheral management, communication management, task management, and humancomputer dialogue. Therefore, the programming platform of the ternary optical computer should enable the programmer to accomplish the application of the new features of the ternary optical processor, but also be fully compatible with the traditional programming environment. Therefore, the basic idea of building a ternary optical computer programming platform is to add to the existing programming environment ways for programmers to use the new features of the ternary optical processor. The main difference between the programming platform of the ternary optical computer constructed in this paper and the traditional computer programming platform is that the programmer no longer uses a single instruction to arrange the working order of each part of the processor and the timing and direction of data transmission to match it. Instead, the programmer only requests the calculation and the number of data bits required, and gives all the raw data to the ternary optical computer management program, which controls and allocates the processor's data bit resources and calculates the raw data. This paper introduces the basic theory and implementation mechanism of this new programming platform, and gives application examples to prepare conceptually, theoretically and technically for the application stage of the ternary optical computer. This paper has the following key contributions: a.It presents the theoretical basis and core technologies for building a programming platform for ternary optical computers.
b.It constructs the operation-data files containing data and computational requirements, and expands on high-level programming languages.
c.It constructs the implementation mechanism of the ternary optical computer programming platform based on these proposed theories.
The remainder of this paper is as follows. In section II, we introduce the ternary optical processor architecture and the MSD adder. Section III presents the theoretical basis of the ternary optical computer programming platform., including: building a model-simple structured data type computer to express the application characteristics of the ternary optical computer, building an operation-data file containing data and computational requirements, expanding the operationdata file transfer instructions. Section IV constructs a ternary optical computer programming platform based on the theory and techniques proposed in the first half of the paper. Section V verifies the effectiveness of the new programming platform through experiments. Finally, Section VI draws conclusion to this research paper.

II. RELATED WORK A. TERNARY OPTICAL PROCESSOR
Light has three stable physical states: dark light state, horizontally polarized light bright state, vertically polarized light bright state, ternary optical computer with these three stable physical light states to represent the information, that is, the source of the ''three values''. Due to the noninterference of light rays, multiple rays can cross each other in the same space without interfering with each other, so many signal rays can be arranged together without isolation to form a flat image. Generally, it takes a few pixels on a flat image to make up a single bit of optical information [13], but optical array devices have many pixels, thus creating the characteristic of a large number of bits of optical computer data. The current experimental system of ternary optical processors constructed with liquid crystal dot matrix screens can have 16384 bits of data, which far exceeds the number of data bits of electronic computers [9], [14]- [16].
The ''reconfigurable processor hardware'' feature is derived from the construction of the ternary optical logic processor-Decrease-radix design principle [17]. The Decrease-radix design principle states that if the n physical states that represent information contain D states, then n(n × n) two-input n-value operators without input (borrowing) can be selected from n×n×(n−1) basic base operation units in a fixed step with no more than n × (n − 1) combinations. where the D-state is the physical superposition with any physical state λ resulting in a physical state that is still a λ-state. The basic operation units is an n-valued logical operator where only one combination of input data makes its output non-Dstate and all other combinations of input data make its output D-state [7], [18]. Applying the theory to the ternary optical computer, no more than six of the 18 basic operation units can be combined to form any of the 3 9 = 19,683 ternary logical operators. Figure 1 shows the optical structure of the ternary optical operation unit. In this structure, the input signal a is input to the main optical path, which consists of two polarizers (P1 and P2) sandwiching a liquid crystal pixel (LC), and the other input signal b is input to the control optical path [19]. There are three types of control optical paths, two semi-reflectors f1 and f2 and a total reflector F for the input optical signal b beam splitting and the upper split is a vertical polarizer V. Only when b is horizontally polarized light, the photocell g2 outputs a high level. There is no polarizer in the lower splitter, as long as b is bright, either horizontally polarized light or vertically polarized light, the photocell g3 will output high level. Based on the values of reconfiguration instructions k2 and k3, the device S will select one of the output signals of g1, g2 and g3 and send the selected signal to the XOR gate Y. When the k1 bit of the reconfiguration instruction is 1, Y takes the signal from the inverse S output, and when k1 is 0, it does not take the inverse. The output signal of Y controls the spin characteristics of the liquid crystal LC in the main optical path [20]. According to the polarization direction of polarizers P1 and P2, the main optical path is divided into four types: P1 is a vertical polarizer while P2 is a horizontal polarizer called VH type, P1 is a horizontal polarizer while P2 is a vertical polarizer called HV type, P1 and P2 are both vertical polarizers called VV type, and P1 and P2 are both horizontal polarizers called HH type.

B. MSD ADDER
The MSD parallel adder is based on a redundant representation of binary values in three symbols. Using this redundant representation of values inside the computer can speed up numerical computation by eliminating the feed delay of the adder. In 1961, Avizienis proposed MSD (Modified Signed Digit) digital expression, an addition technique capable of eliminating the rounding delay [21]. Any decimal number A can be represented by an MSD number as follows: Among them, the value range of k i ∈ {u, 0, 1} and i is an integer, the symbol u stands for -1. MSD is also a binary counting. So there can be multiple MSD representations for a decimal number. In the TOC, the three states of light(horizontally polarized light, vertically polarized light and no light) are used to represent 1, 0 or -1. The transition between these three states is realized by the liquid crystal and polarizer to complete the corresponding operation.
MSD addition uses four logical operations, called T, W, T' and W', as shown in Table 1 (a and b are the input data, and T, W, T', W' and M are the output data after the corresponding truth table conversion). An MSD addition operation is completed in 3 steps as follows: Step 1. T, W operations are performed on input data a and b simultaneously, and the result of T is shifted one place to the left.
Step 2. Perform both T' and W' operations on the result of the first step, and shift the result of T' one place to the left. Step 3. The result of the second step is T, and the result is the sum of a and b.

III. THEORETICAL BASIS OF TERNARY OPTICAL COMPUTER PROGRAMMING PLATFORM A. SIMPLE STRUCTURED DATA TYPE COMPUTER
The number of data bits in a ternary optical computer is on the order of thousands, and the long delay caused by the rounding process in a serial rounding adder is undoubtedly a waste of resources, so it is necessary to construct a full adder with no rounding operation. Ordinary electronic computers have only two values: 0 and 1, so electronic computers cannot use the simple structure and parallel operation of the MSD adder. There are three bases for constructing a ternary optical MSD adder: (1) The ternary optical computer has three stable physical states. (2) The per-bit reconfigurability of the ternary optical logic processor. (3) The large number of data bits of the ternary optical processor. These three foundations dictate that the use of binary MSD adders for ternary optical computers becomes inevitable. The MSD adder extends the ternary optical computer from a logic operator to an optical adder, and the processor can be reconfigured into a composite operator containing a three-valued logic operator, a twovalued logic operator, an adder, and a subtractor.
If a processor is capable of directly computing structured data types, this processor must have many data bits and the data bits must be groupable for independent use to cope with the number of data bits for each component of the structured quantity. At the same time, the computational functions of the individual data bits of this processor must be reconfigurable at runtime as required by the programmer to cope with the different computational requirements of each component of the structural data type. The ternary optical processor satisfies these conditions, and the reconfigured composite processor can compute all components of a simple structural data type simultaneously when executing a single instruction. Therefore, the application characteristics of ternary optical computers are summarized as ''simple structural data type computers''. Among them, the structural data type is a composite data type customized by the programmer for the characteristics of the application problem, which contains multiple components, each with different data bits and calculation rules.
The simple model of ''simple structural data type computer'' is used to characterize the application of the ternary optical computer, which is easy for programmers to accept, understand and apply this new type of computer. More details about structural data type computers can be found in the literature [22].

B. OPERATION-DATA FILE
Ternary optical computer takes several clock cycles to reconfigure the processor, if only a small amount of data is computed after reconfiguring the processor, the time spent reconfiguring the processor is comparable to the time spent computing the data. If a large amount of data is computed after reconfiguring the processor once, the time to reconfigure the processor is negligible.
The most intuitive representation of optical processor utilization is the percentage of data bits effectively used per unit of time on average. It consists of the ratio of the number of data bits effectively used in one operation to the total number of data bits and the ratio of the processor reconstruction time to the total operating time. Let the operator reconfiguration operation takes q computation cycles, N y computations are completed between reconfigurations, the total number of data bits is n, and the number of efficiently used data bits is m . Then, the effective use ratio B of data bits is: where m × Ny is the amount of computation done by the optical processor after one reconstruction. The product of the channel width m of the task and the number of data N is called the computational volume of the task. From equation (2), the utilization rate is constrained by q when Ny is not much larger than q. This indicates that tasks with very little computation are not suitable for running on a ternary optical computer. Therefore, the ternary optical computer is more suitable for processing the same operation for large amounts of data, which requires the user to send the reconfiguration requirements for the processor and a large amount of initial data to the ternary optical processor at the same time when using the ternary optical computer. In addition, a single computing task rarely uses a processor of thousands of bits, and it is often necessary to share the processor among several computing tasks. Therefore, the calculation rules and raw data are sent to the CPU at program runtime and are not applicable to ternary optical processors. To adapt to the characteristics of the ternary optical processor, it is proposed to organize the computational tasks and raw data into a dedicated file-the operation-data file, also known as *.SZG file (* is a generic character for the file name), and then send the SZG file to the optical processor in the application program in due time to calculate. SZG files are the only way for users to exchange information with ternary optical computers when processing large volumes of data [23], [24]. To ensure that the ternary optical processor is informed of the user's computational intent accurately, the programmer must maintain a consistent format both when constructing the SZG file and when the ternary optical computer parses the SZG file. As shown in Figure 2, the SZG file consists of two parts: the indication section and the data section. The indicator section records the general information and necessary parameters of this SZG file, which is an important basis for processor reconstruction and data bit allocation by the ternary optical processor, and the data section contains all the raw data in the SZG file. We have developed a helper software to help users generate SZG files accurately. This auxiliary software provides a user-friendly input interface, as shown in Figure 3. The user only needs to input the required arithmetic requirements and raw data on this interface, and the software will automatically generate the corresponding SZG files according to the information input by the user and save them to the specified path. The SZG file interaction can be summarized as: Step 1. The programmer generates the SZG file through the auxiliary software, sends the SZG file to the task management software of the ternary optical computer when the application is running, and makes the application wait for all the calculation results to be returned, when the application can handle other tasks.
Step 2.The ternary optical computer task management software parses the received SZG file, assigns the corresponding number of data bits from the ternary optical processor to this SZG file, and reconstructs these data bits into the calculator requested by the SZG file.
Step 3. The task management software sends the raw data in the SZG file to the reconstructed calculator one by one and collects the results until all the data in the SZG file has been calculated.
Step 4. The task management software organizes all calculation results into a result SZG file (-R.SZG file) and returns the -R.SZG file to the corresponding application.
Step 5. The application receives the -R.SZG file and processes the data in the result file for the next step.
In this process, the programmer only generates the SZG file in Step 1 and removes the data from the -R.SZG file in Step 5, while the operations on the ternary optical computer hardware and the underlying software are done by the task management software. Thus, the SZG file obscures the complexity of the ternary optical processor hardware and underlying software for the programmer, and users can more easily use the application features of the ternary optical computer.

C. EXPANSION OF THE HIGH-LEVEL PROGRAMMING LANGUAGE FOR TOC
The SZG file translates the programmer's control of the ternary optical processor into the task of transmitting the SZG file to ternary optical computer. The instructions to control the ternary optical processor are thus simplified to transfer SZG file instructions. So we simply package the program module that transfers the SZG file to the specified target device into an expanded instruction for the current programming language -the transfer SZG file instruction. This expansion command can be used in the application to send the specified SZG file to the ternary optical computer at the right time. The goal of having the few-bit electronic processor where the application is located and the many-bit optical processor where the SZG file calculation task is done work together in one application. Expansion instructions for transferring SZG files have been successfully developed for C, MPI parallel programming language and C++ [25]- [27]. The following is an example of the core technical steps for the expansion of a high-level programming language, using the expansion statements in C as an example.
Step 1. void SZG_Init(). Creates the expansion instruction environment for a ternary optical computer. It must be the first expansion instruction that appears in the program and can only appear once in a program.
Step 2. int SZG(char * path). Sends the *.SZG file referred to by path to the ternary optical processor. The return value of this instruction is the code for the success or failure of this send, and the programmer can decide the next action based on this return value.
Step 3. int SZG_SearchResult(char * path). Address pointer path refers to the *.SZG file running status on the ternary optical processor. The return value ''0'' indicates that the receiving process has obtained the calculation result file *-R.SZG and stored it in the same directory as the *.SZG file. The return value of ''1'' means that the result file *-R.SZG is not yet available. The return value of ''2'' indicates that there is an error on the side of the ternary optical processor and the processing of *.SZG files has been stopped, so no result is returned.
Step 4. void SZG_Suspend(). Hangs this program until it is woken up when the operation result file returns. In addition, the application must also add the header file ''#SZG.h'' before proceeding with the above steps. Figure 4 gives a general view of the ternary optical computer programming platform, which consists of two computing cores, the electronic processor and the ternary optical computer (TOP). Based on these two core underlying software, the operating system (OS), the program language of the SZG file transfer instruction is extended, and the SZG file generation software, a complete programming platform is formed. As shown in Figure 4, the ternary optical computer implicitly contains an auxiliary processor with the same status as the ternary optical processor. This auxiliary processor is used to perform various transactional tasks such as memory management, task management of TOC, I/O operation management, SZG file transfer, and ternary memory management for optical processor reconfiguration planning, etc. Together with the TOC task management software, processor reconstruction software and SZG file transfer software, this constitutes the underlying software system of the TOC.

IV. IMPLEMENTATION MECHANISM OF THE TOC PROGRAMMING PLATFORM
The basic idea and process of preparing a ternary optical computer application by applying the ternary optical computer programming platform can be described as follows: Step 1. Modeling. Mathematical description of the application problem according to the structural data type computer.
Step 2. Organize the data. Determine the types of data and the corresponding operation rules. Determine constants, variables, arrays, structures, and enumerations, and determine the rules for calculating each of them. We also need to organize the raw data and the corresponding operation rules into SZG files.
Step 3. Design the program structure. Draw data flow diagram, draw control flow diagram, write pseudo code program, etc.
Step 4. Write the program. Write the instruction sequence. A typical instruction sequence can be described as follows: Initialization instructions (define and initialize variables, system environment, etc., including establishing the operating environment for SZG expansion instructions); Input Data Instruction; Judgment Status Instruction; Send the data in pairs to the operator for calculation and obtain the result instruction (e.g.C = A + B); Send the data to a mathematical function routine to calculate and obtain the result instruction (e.g. Sin(45, C)); The expansion instruction that sends the SZG file to the ternary optical processor for calculation (e.g. int SZG Send(char * path)); Waiting for the TOC to send back the calculation results of the expansion command (e.g. void SZG Suspend()); . . . . . . Judgment result instruction sequence; Instructions that determine the flow of the program; Sequence of instructions to output program results; End of program. Obviously, the four main steps above are the same as the current programming process exactly. Only the concept of ''TOC is a simple structural data type computer'' is added in the first step. Added the method of ''organizing a large amount of raw data into SZG files'' in the second step. The fourth step adds the expansion instruction ''Send the SZG file to the TOP and wait for the calculation result file to return''. Thus the programming platform of TOC is built on the theoretical basis of the simple structural data type computer concept, SZG files and high-level language expansion instructions proposed in our paper.
In addition, by observing the above steps, the TOC programming environment constructed by this method contains the complete, unaltered electronic computer programming environment. Therefore, in this environment, we can prepare electronic computer applications as usual, and the original electronic computer programs can also run normally intact, moreover, we can prepare electronic computers and ternary optical processors to work together as an application.

V. EXPERIMENT AND ANALYSIS A. EXPERIMENT ENVIRONMENT
This experiment uses a TOC prototype system SD16 of No. 1, whose shape is shown in Figure 5. Among them, the TOP (the black area with the bright spot in the middle) has 576 pixels in the LCD array, arranged in a 24 × 24 array. Three adjacent pixels in each row constitute one bit of the optical processor, so this experimental device has a total of 192 processor bits, meaning that 192 bits of data can be processed in parallel [28]. The liquid crystal is arranged as shown in Figure 6, the liquid crystal is divided into two parts, with the processor bits in each part numbered as shown by the arrows in Figure 6. Coworking electronic computer hardware configuration: CPU: Intel(R) Core(TM) i5 430 M 2.26 GHz. RAM: 4 GB. OS: 64-bit windows 8. Expanded programming language for SZG file transfer instructions: C.

B. TEST CASES AND RESULTS ANALYSIS
In order to test the correctness of the TOC programming platform, we will implement four functions in our program. For example, calculate f 1 = a+b, f 2 = c−d, f 3 = e∧g and independent variables a, b, c, d, e, g, h, and  ihave 17,17,12,12,19,19,20,20 bits, and each variable has 10,000 data. Given the length of the paper, the first eight sets of MSD numbers for the four functions are given in Table 2, and the subscripts are labeled with M .
In a ternary optical computer, the data of f 1, f 2, f 3 and f 4 are first formed into simple structural data types. We can represent this simple structured data type by A. It has four subtypes (terms), namely: 17-bit floating-point type A(1), 12-bit floating-point type A(2), 19-bit logical type A(3) and 20-bit logical type A (4). The SZG file is then generated, and the SZG file format is instantiated as follows:   fed into the ternary optical computer via the C expansion command.
After the ternary optical computer receives the Ls.SZG file, the task management software of the optical processor reconfigures the composite processor based on the data bit information given in the Ls.SZG file. The first 89 bits of the complex processor are constructed as an adder, 89 to 152 bits as a subtractor, the next 19 bits as an ''AND'' operator, and the last 20 bits as an ''OR'' operator. Among them, corresponding to the 192 processor bits of the optical processor, the allocation of processor bits for the 5 transforms of the adder corresponding to f 1 and f 2 is shown in Table 3, and the allocation of processor bits for f 3 and f 4 is shown in Table 4. Then the first pair of raw data of A(1), A(2), A(3) and A(4) is fed into the reconstructed simple structured data type processor. Run the calculation instruction once to get a set of f 1, f 2, f 3 and f 4 results, and so on. 10,000 times in parallel can be executed to complete the calculation of all 40,000 pairs of data and get the final result. Figure 7 shows the theoretical output results for the first 8 groups of the 4 functions in the experimental use case. Figure 8 shows the resultant images of the optical processor output corresponding to the theoretical results. Table 5 shows the theoretical results in the form of MSD. where, in Figure 8, the high luminance points are pixels that output vertically polarized light (V state), the low luminance points are  pixels that output horizontally polarized light (H state), and the rest of the points are pixels that output no light state (W state). Taking the first set of results of the addition operation as an example, it can be seen from Table 3 that bits 70-88 of the operator are the result area, which corresponds to the area marked in yellow in Figure 9. White represents vertical light and is represented by 1, blue represents horizontal light and is represented by u, black represents no light and is represented by 0. According to the decoding principle of the ternary optical computer, the number of liquid crystal bits is from left to right and from top to bottom, and every 3 liquid crystal pixels represent one processor bit, in the order of (0010u1u010u1u000u00) M = (54076) D , which is consistent with the theoretical results. We analyzed the rest of the results and obtained all accurate results. Experiments show that the SZG file generated by the electronic computer can be correctly parsed by the ternary optical processor, that the high-level language expansion instructions can deliver the SZG file to the ternary optical processor correctly, and that the ternary optical processor can compute data of a structural data type correctly. Therefore, the programming platform proposed in this paper can make use of the computational features of the ternary optical computer while allowing programmers to maintain their traditional programming habits and build a bridge between the user and the ternary optical computer.

VI. CONCLUSION
The ternary optical computer provides us with a massively data-parallel computing tool through three major application features: the large number of data bits, the fact that the data bus can be used independently in arbitrary groups, and the fact that the computational function of each data bit can be reconfigured at runtime. However, current programming platforms are built around traditional processors such as electronic CPU, GPU or MIC, and are unable to produce applications that take advantage of the strengths of ternary optical computers. The theory and techniques presented in this paper give a basic scheme for constructing programming environments for new computer systems. The new programming environment is not only fully compatible with traditional programs, but also maintains the programmer's work and thinking habits very well, making it easy for programmers to accept and develop new programs that work with traditional processors and new processors. Meanwhile, when other new computers are to enter the application stage, the corresponding programming models can be constructed by drawing on the ideas we propose in this paper.

DISCLOSURES
The authors declare that there are no conflicts of interest regarding the publication of this paper.